... my %best; while (my $line=) { chomp $line; my ($item,$group,count)= split(/\s+/,$line); if ((not exists $best{$group}) or $best{$group}{count}<$count) { $best{$group}{count}=$count; $best{$group}{item}= $item; } } #to print them out foreach my $group ( keys %best) { print "$best{$group}{item} $group $best{$group}{count}\n"; }