use Data::Dumper; my $struct; while(my $line = ){ chomp $line; next if $line =~ m/^\s*$/; my ( $key, $root, $parent, $current, $duration, $function) = parse_csv( $line ); push @{$struct->{$key}}, { root => $root, parent => $parent, current => $current, duration => $duration, function => $function, } } print Dumper $struct; # get all the 13 functions print "13 functions\n"; for my $ref( @{$struct->{13}} ) { print $ref->{function}, $/; } # this is a quick and dirty way to parse well formed CSV # use Test::CSV_XS if it is not well formed/trivial sub parse_csv { my ( $line ) = @_; $line =~ s/^\s*"|"\s*$//g; return split /"\s*,\s*"/, $line; } __DATA__ "1", "2", "3", "4", "100" , "A" "10", "20", "30", "40", "200" , "B" "11", "21", "31", "41", "300" , "C" "12", "22", "32", "42", "400" , "D" "13", "23", "33", "43", "500" , "E" "13", "23", "33", "53", "600" , "F" "13", "23", "33", "63", "700" , "G" "13", "23", "34", "73", "800" , "H" "13", "23", "34", "83", "900" , "I" "13", "24", "35", "93", "1000" , "J" "13", "24", "36", "103", "1100" , "K" __END__ $VAR1 = { '13' => [ { 'function' => 'E', 'parent' => '33', 'current' => '43', 'root' => '23', 'duration' => '500' }, { 'function' => 'F', 'parent' => '33', 'current' => '53', 'root' => '23', 'duration' => '600' }, { 'function' => 'G', 'parent' => '33', 'current' => '63', 'root' => '23', 'duration' => '700' }, { 'function' => 'H', 'parent' => '34', 'current' => '73', 'root' => '23', 'duration' => '800' }, { 'function' => 'I', 'parent' => '34', 'current' => '83', 'root' => '23', 'duration' => '900' }, { 'function' => 'J', 'parent' => '35', 'current' => '93', 'root' => '24', 'duration' => '1000' }, { 'function' => 'K', 'parent' => '36', 'current' => '103', 'root' => '24', 'duration' => '1100' } ], '1' => [ { 'function' => 'A', 'parent' => '3', 'current' => '4', 'root' => '2', 'duration' => '100' } ], '10' => [ { 'function' => 'B', 'parent' => '30', 'current' => '40', 'root' => '20', 'duration' => '200' } ], '11' => [ { 'function' => 'C', 'parent' => '31', 'current' => '41', 'root' => '21', 'duration' => '300' } ], '12' => [ { 'function' => 'D', 'parent' => '32', 'current' => '42', 'root' => '22', 'duration' => '400' } ] }; 13 Functions E F G H I J K