Posted By

alfirth on 03/04/11


Tagged

parse csv hash hashref textcsv


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jeremydouglass


Text::CSV by hashref


 / Published in: Perl
 

  1. use Data::Dumper;
  2. my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute.
  3. or die "Cannot use CSV: ".Text::CSV->error_diag ();
  4. open my $fh, "<:encoding(utf8)", "$infile" or die "$infile $!";
  5.  
  6. $csv->column_names ($csv->getline ($fh));
  7.  
  8. while ( my $hr = $csv->getline_hr( $fh ) ) {
  9. $hash{ $hr->{fieldA} }= $hr->{valueA};
  10. }
  11.  
  12. $csv->eof or $csv->error_diag();
  13. close $fh;
  14.  
  15. print Dumper(%hash)

Report this snippet  

You need to login to post a comment.