http://www.perlmonks.org?node_id=625258


in reply to Read the csv file to a hash....

Hmm, this sounds like fun homework. :)

My solution doesn't deal with "ragged" .csv's -- if there's a line with more entries than the others, things will get thrown off. That's easy to fix, I'll leave that to you.

use strict; use warnings; use Data::Dumper qw(Dumper); open(INPUT,"foo.csv") or die "Can't open foo.csv, $!"; my @columns; my %hash; while(<INPUT>) { chomp; my $index=0; if(/"/) { push @{$columns[$index++]},$2 while /("+)(.*?)\1/g; } else { push @{$columns[$index++]},$1 while /([^,]+)/g; } } my @headings=map {shift @$_} @columns; print "Headings: @headings\n"; @hash{@headings}=@columns; print "Hash contents:\n"; print Dumper(\%hash);

Mike