my %count; foreach (@array2) { my ($id) = split /,/; $count{$id}++; } foreach (sort @array2) { my ($id) = split /,/; print "$id: $count{$id}\n"; }