use strict; # complain about errors my %lexicon; # collect unique strings while (<>) { # read each line input my @words = # gather list of words map { s/^\W+|\W+$//g; $_ } # strip punctuation split /\s+/; # break line at spaces foreach my $word (@words) { # each word in the line $lexicon{$word}++; # should be counted } } print "$_: $lexicon{$_}\n" # show word and count foreach (sort keys %lexicon); # for all words in order