vagabonding electron has asked for the wisdom of the Perl Monks concerning the following question:
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 @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
The error message is: Expected fields to be an array ref at ...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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to print after using getline_hr (Text::CSV_XS)?
by Tux (Canon) on Nov 09, 2011 at 11:46 UTC | |
by vagabonding electron (Curate) on Nov 09, 2011 at 13:08 UTC | |
|
Re: How to print after using getline_hr (Text::CSV_XS)?
by jethro (Monsignor) on Nov 09, 2011 at 11:55 UTC | |
by vagabonding electron (Curate) on Nov 09, 2011 at 13:13 UTC |