use strict; use Data::Dumper; my @all; my %occurances; while () { chomp $_; push @all, [ split(/\|/,$_) ]; # I should explain what is going on here # the -1 is going to get the last element # in a list. In this case the first [-1] # is the last list of elements added # from the DATA section below with the push # function. The second -1 # is the last element of that list which # is the class. The next key in the # occurances hash is the name_? value # which is extracted from the last array # (-1) pushed onto the @all and the first # element (0) of the annoymous array in # in that (-1) location of @all. $occurances{$all[-1][-1]}{$all[-1][0]}++; # class # name_? } print Dumper(\@all); print Dumper(\%occurances); __DATA__ name_x|score|date|class1 name_y|score|date|class2 name_y|score|date|class2 name_a|score|date|class2 name_b|score|date|class3 name_z|score|date|class1 name_b|score|date|class3 name_x|score|date|class1 name_b|score|date|class3 name_c|score|date|class2 name_c|score|date|class3 name_c|score|date|class3