#!/usr/bin/perl use warnings; # This is the same as wk4_exercise.pl while (<>) { @new_words = split /\s+|[".,()!]/, $_; foreach (@new_words) { if ($_) { s/(.*)/\L$1/g; $word_count{$_}++; } } } # The new sorting expression my @keys = sort by_count keys %word_count; # Sorting subroutine sub by_count { $word_count{$a} <=> $word_count{$b} or # By increasing value $a cmp $b # Fallback to ASCIIbetical (note that when } # creating the hash we converted to lower case # so ASCIIbetical will work fine here) # Same print statement as wk4_exercise.pl foreach (@keys) { printf "%-20s %3s\n", $_, $word_count{$_}; }