my $top = 5; foreach $w ( (sort { $count{$b} <=> $count{$a} } keys %count)[0..$top-1] ) { print "$count{$w} $w\n"; }