Wordcount.pl
#!/usr/bin/perl
# prints out a list of frequencies and words of an arbitrary text file
$/ = ""; # paragraph mode on
$* = 1; # multi-line patterns on
while (<>) {
#s/-\n//g; # remove hyphens
tr/A-Z/a-z/; # Canonicalize to lower case.
@words = split(/\W*\s+\W*/, $_); # split into words
foreach $word (@words) {
$wordcount{$word}++; # count the words
}
}
# print out the totals sorted in alphabetical order
foreach $word (sort keys(%wordcount)) {
printf "%8d\t\t%s\n", $wordcount{$word}, $word;
}
# ************************************************************************
# Copyright 1997 Ben S.
# Free Software, however all rights reserved.
# Redistribution is permitted provided that -
# (a) this copyright notice is duplicated in all such forms
# (b) that the program is distributed in its original unmodified form.
# You may freely modify this software, provided you indicate this.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
# ************************************************************************