Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

how to get out data of a hash?

by blaui (Initiate)
on Apr 17, 2018 at 11:17 UTC ( #1213055=perlquestion: print w/replies, xml ) Need Help??

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 (Archbishop) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1213055]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-11-26 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?