#!/share/bin/perl -w # A Perl classic, tiny progam to count all occurrences # of distinct words in a document. open(INPUT, "films.txt") || die "Coldn’t read films.txt"; while($line = ) { @words = split /[^A-Za-z]/ , $line; foreach $word (@words) { # Can get 'empty' words where have # punctuation marks etc if($word eq "") { next; } $index = lc $word; $counts{$index}++; } } # This code sorts them alphabetically # @sortedkeys = sort keys %counts; # foreach $key (@sortedkeys) { # print $key, "\t: ", $counts{$key}, "\n"; # } # While this code gives them in ascending order by frequency foreach $key (sort { $counts{$a} <=> $counts{$b} } keys % counts) { print $key, "\t:", $counts{$key}, "\n"; }