#!/usr/bin/perl open(IN,"/some/file") || die "Cant open file\nReason: $!\n"; while () { chomp($line = $_); ($first,$second) = (split(/:/, $line))[1,2]; $fruit{$first}{count}++; $fruit{$first}{$second}++; } close(IN); foreach $k (sort(keys(%fruit))) { print "$k $fruit{$k}{count}\n"; }