Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Dear Monks,
after some experience in parsing the csv files with split & join I try to use the modul Text::CSV_XS now. I get a problem while trying to print the file if I use getline_hr.
A bit of case history. I need to modify some values in some columns. These columns have always the same name (header) but the are at the different positions in the different csv files. I must therefore access these columns by names.
The first script below works - because I take every column by name and combine them later. This cannot be a solution because as said the required columns can be at different positions in the different files and the number of columns can be different too.
use strict; use warnings; use 5.010; use Text::CSV_XS; my $csv = Text::CSV_XS->new( { binary => 1, sep_char=>';' } ) or die " +Cannot use CSV: ".Text::CSV->error_diag(); my @fields = ('ID', 'NAME','INDICATOR','VALUE','CI_LOW', 'CI_HIGH'); $csv->print( \*STDOUT, \@fields ); say ''; $csv->column_names( $csv->getline( *DATA ) ); while ( my $hr = $csv->getline_hr( *DATA ) ) { my @nr = @{$hr}{'ID'}; s/12/Sun/g for @nr; s/17/Moon/g for @nr; my @name = @{$hr}{'NAME'}; my @ind = @{$hr}{'INDICATOR'}; my @val = @{$hr}{'VALUE'}; my @low = @{$hr}{'CI_LOW'}; my @high = @{$hr}{'CI_HIGH'}; if ($csv->combine ( @nr, @name, @ind, @val, @low, @high)) { print $csv->string, "\n"; } else { print "combine () failed on argument: ", $csv->error_input, "\n"; } } __DATA__ ID;NAME;INDICATOR;VALUE;CI_LOW;CI_HIGH 12;Name1;01;95;89;100 12;Name1;02;75;50;92 12;Name1;03;89;82;96 12;Name2;01;99;98;100 12;Name2;02;95;90;100 12;Name2;03;22;12;32 17;Name1;01;93;83;95 17;Name1;02;78;62;96 17;Name1;03;37;17;57
If I try it the other way the script shows an error:
use strict; use warnings; use 5.010; use Text::CSV_XS; my $csv = Text::CSV_XS->new( { binary => 1, sep_char=>';' } ) or die " +Cannot use CSV: ".Text::CSV->error_diag(); my @fields = ('ID', 'NAME','INDICATOR','VALUE','CI_LOW', 'CI_HIGH'); $csv->print( \*STDOUT, \@fields ); say ''; # $csv->column_names( $csv->getline( *DATA ) ); while ( my $hr = $csv->getline_hr( *DATA ) ) { # my @nr = @{$hr}{'ID'}; s/12/Sun/g for @{$hr}{'ID'}; s/17/Moon/g for @{$hr}{'ID'}; $csv->print (*STDOUT, $hr) or $csv->error_diag; } __DATA__ ID;NAME;INDICATOR;VALUE;CI_LOW;CI_HIGH 12;Name1;01;95;89;100 12;Name1;02;75;50;92 12;Name1;03;89;82;96 12;Name2;01;99;98;100 12;Name2;02;95;90;100 12;Name2;03;22;12;32 17;Name1;01;93;83;95 17;Name1;02;78;62;96 17;Name1;03;37;17;57
The error message is: Expected fields to be an array ref at ...
It is correct since by using geline_hr it is hash reference not array reference.
I tried it the way as [%$hr] - but no success.
Your help is very much appreciated!
Thanks in advance.
VE

In reply to How to print after using getline_hr (Text::CSV_XS)? by vagabonding electron

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2015-07-08 04:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (94 votes), past polls