jjohhn has asked for the wisdom of the Perl Monks concerning the following question:
C:\scripts>wordcount.pl alice.txt
ALICE'Sdistinct words: 4007
frequency of most common word: 5160
common word:
use strict; my $maxcount; my $find; sub read_line { our %hash; my @list = split /[,.?!)"]?\s\)?/, shift; my $count; foreach my $word (@list) { $hash{lc $word}++; $count =$hash{lc $word}; if ($count > $maxcount) { print "$word"; $maxcount++; $find = $word; } } } sub read_file{ my $file=shift; open (FILE, $file) or die "couldn't open $file: $!"; while (my $line = <FILE>) { read_line $line; } } read_file @ARGV; my $numwords= keys our %hash; print "distinct words: $numwords\n"; print "frequency of most common word: $maxcount\n"; print "common word: $find";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using hash to find frequency count
by ikegami (Patriarch) on May 15, 2005 at 03:30 UTC | |
by jjohhn (Scribe) on May 15, 2005 at 03:57 UTC | |
by jjohhn (Scribe) on May 15, 2005 at 04:07 UTC | |
by jjohhn (Scribe) on May 15, 2005 at 04:23 UTC | |
by ikegami (Patriarch) on May 15, 2005 at 13:44 UTC | |
|
Re: using hash to find frequency count
by jpeg (Chaplain) on May 15, 2005 at 03:32 UTC | |
by ikegami (Patriarch) on May 15, 2005 at 03:37 UTC | |
|
Re: using hash to find frequency count
by ghenry (Vicar) on May 15, 2005 at 11:39 UTC | |
|
Re: using hash to find frequency count
by tlm (Prior) on May 15, 2005 at 14:54 UTC |