my %count; while ( ) { $count{lc $_}++ for /\w+/g; } print "$_ => $count{$_}\n" for sort { $count{$b} <=> $count{$a} || $a cmp $b} keys %count;