in reply to Word Frequency counter
For a start
while (@thisfile) {
doesn't do what you think. In particular, it is not
for (@thisfile) {
Next:
$seen{b} <=> $seen{a}
compares the values for keys 'a' and 'b', not for the two sort variables' ($a and $b) contents as you are hoping. Cleaning those problems up, fixing a few other style issues and providing some sample data gives:
use strict; use warnings; my $fileData = <<DATA; Greetings, I've been trying to put a word frequency counter together but I keep g +etting an unitialised value in pattern match. I'd be grateful if somebody could +let me know what I'm dong wrong. I added a line to get some repeated words. DATA my %seen; open my $inFile, '<', \$fileData; for (grep {chomp; length} <$inFile>) { $seen{lc $1}++ while /(\w['\w-]*)/g; } close ($inFile); printf "%5d %s\n", $seen{$_}, $_ for sort { $seen{$b} <=> $seen{$a} } +keys %seen;
Prints:
2 a 2 to 2 i 1 i've 1 know 1 put 1 if 1 unitialised 1 greetings 1 i'd 1 frequency 1 wrong 1 let 1 could 1 in 1 keep 1 line 1 repeated 1 trying 1 what 1 value 1 me 1 match 1 grateful 1 i'm 1 word 1 be 1 some 1 somebody 1 but 1 added 1 words 1 dong 1 been 1 get 1 together 1 getting 1 pattern 1 counter 1 an
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Word Frequency counter
by Anonymous Monk on Oct 03, 2008 at 07:53 UTC |