while () { my $c = () = /\Q$bit\E/g; $hash{$_} = $c if $c; } my @rank = sort{ $hash{$b} <=> $hash{$a} } keys %hash; my $to_print = 10; $to_print = @rank if @rank < 10; print "$_: $hash{$_}\n" for @rank[0..$to_print-1];