my %g = (); my @innerKeys = qw/ specie gener age haircolor /; while() { my @vals = split /,/, $_; my $name = shift @vals; @{$g{$name}}{ @innerKeys } = @vals; } #### my $csv = Text::CSV->new(); open FILE, $filename or die "couldn't open file '$filename': $!"; # if the file has colnames in the first row, do this: $_ = ; $csv->parse($_); my @cols = $csv->fields(); # otherwise, manually define them: my @cols = qw/ name specie gener age haircolor /; while(){ # let Text::CSV do the hard work. $csv->parse($_); # this turns blanks into undef's -- remove the "map {}" part if blanks are ok. my @vals = map { !defined($_) || $_ eq '' ? undef : $_ } $csv->fields(); # This hashes the whole row my $row = { map { $cols[$_] => $vals[$_] } 0 .. $#cols }; # Now do whatever you need with it. $g{ $row->{name} } = $row; # Or a common/generic usage might be: push @rows, $row; }