http://qs321.pair.com?node_id=1213055

blaui has asked for the wisdom of the Perl Monks concerning the following question:

Hello!
I put data to a hash, but then I want to get it out as kind of array/list;
push @{$csvheader{$pre}}, $tmp; print "@{$csvheader{'x_'}}\n"; $tmp = "paste -d';' ". @{$csvheader{'x_'}} ." > all.x.txt"; print "$tmp\n"; system($tmp);
The first print gives, what I expected:
wallshearstress.txt wallshearstress_l.txt wallshearstress_u.txt wallheatflux.txt wallheatflux_l.txt wallheatflux_u.txt

But the second print gives:
paste -d';' 6 > all.x.txt

How I have to modify, that the second print gives:
paste -d';' wallshearstress.txt wallshearstress_l.txt wallshearstress_u.txt wallheatflux.txt wallheatflux_l.txt wallheatflux_u.txt > all.x.txt

Regards, Blaui

Replies are listed 'Best First'.
Re: how to get out data of a hash?
by choroba (Cardinal) on Apr 17, 2018 at 11:26 UTC

    Why?

    Concatenation (.) forces scalar context. Arrays in scalar context return their size.

    How?

    You can either use join:
    $tmp = "paste -d';' " . join(' ', @{ $csvheader{x_} }) . ' > all.x.txt +';

    or just double quote the array, its members will be separated by $" which is a space by default:

    $tmp = "paste -d';' @{ $csvheader{x_} } > all.x.txt";

    Alternative

    Instead of using paste, use Text::CSV_XS to process your CSV files. It handles quoted fields, escaped separators and quotes, and more.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thanx a lot. :)
      "CPAN is your bestest friend." There you will find fast, thoroughly-tested modules for correctly handling all kinds of files and formats – JSON, XML, CSV, you name it. "There's more than one way to do it," but "the best way to do it" is always to "let somebody else do it for you." Just use their module, knowing that it is known to work correctly in all cases.

        'but "the best way to do it" is always to "let somebody else do it for you." Just use their module, knowing that it is known to work correctly in all cases.'

        Not sure if this is sarcasm, but bugs are found, reported and patched in CPAN modules all the time. Letting someone else do it for you, without understanding how things work and what they actually do can hardly be described as 'the best way'.

        A reply falls below the community's threshold of quality. You may see it by logging in.