in reply to Read the csv file to a hash....
#!/usr/bin/perl -w use strict; use Text::CSV_XS; my $fh; unless (open $fh, "filename") { die "No file available\n"; } my $csv = Text::CSV_XS->new({binary => 1}); my @people; <$fh>; # Drop column names on the floor while ( my $line = <$fh> ) { my $status = $csv->parse($line); my @columns = $csv->fields(); my %h; @h{('Name','Comment')} = @columns; push @people, \%h; } close $fh; print "Name: " . $people[2]->{'Name'} . ", Comment: " . $people[2]->{'Comment'} . "\n";
It seems to me that keeping the associated data together is a better solution to this problem. Also, looping by key field is more flexible than looping by dedicated array name.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Read the csv file to a hash....
by RMGir (Prior) on Jul 06, 2007 at 16:16 UTC | |
by snopal (Pilgrim) on Jul 06, 2007 at 16:28 UTC | |
Re^2: Read the csv file to a hash....
by naikonta (Curate) on Jul 06, 2007 at 17:09 UTC | |
by whereiskurt (Friar) on Jul 07, 2007 at 14:13 UTC |