#!/usr/bin/perl use strict; # Yeah, I don't normally do this, but ya wanted readable... # Open the input file my $fileName = shift @ARGV; open INPUT, $fileName or die "Cannot find $fileName\n"; my %wordCount; # The hash to hold the words. # Loop over ieach line in the input while () { # And over each word in the line #while (/\b(\w+)\b/g) # This is my definition of word, which doesn't # match yours. I like mine lots better, but # it's your game, so... while (/(\S+)\s+/g) { $wordCount{lc $1}++ ; } } # Build the results my (@result, $key, $value); # Declaring variables first, like I should while ( ($key, $value) = each %wordCount) { push @result, "$value\t$key\n" } # sort the results # hey, this is a scripting language # I care more about programmer efficiency than program efficiency, so # I'm using the built in sort, customizing the compare @result = sort { ($b <=> $a) || ($a cmp $b) } @result; print @result;