in reply to wisdom needed: sorting an array

Using hashes is one way to tackle the problem, as others have shown. But since your example suggests that the data is clumped - that is, numbers which are the same follow each other, you don't need a hash, as the program below shows.

I'm just showing it here for the sake of showing an alternative way. I don't expect it to be faster - hashes are pretty fast, and the sort is likely to dominate the running time anyway.

Abigail

#!/usr/bin/perl use strict; use warnings 'all'; my @info; while (<DATA>) { my ($num, $info) = split ' ', $_, 2; if (@info && $num == $info [-1] [0]) { $info [-1] [2] ++; } else { push @info => [$num, $info, 1] } } print map {"@{$_}[0, 1]"} sort {$b -> [2] <=> $a -> [2]} @info; __DATA__ 1 info1 1 info1 1 info1 2 info2 3 info3 3 info3 4 info4 4 info4 4 info4 4 info4 $ ./count 4 info4 1 info1 3 info3 2 info2 $