my %top_words = reverse %words #reverse to sort by value as opposed to key %top_words = sort {$top_words{$b} <=> $top_words{$a}} %top_words; # sort z-a to get the highest ones at the top my $counter=0; foreach my $value (%top_words) { if ($counter > 20) {last;} #exit loop print "$top_words{$value}\n"; $counter++; } #print 20 words with highest value