One problem leaps out at me immediately, you aren't chomping your input lines. Sometimes you'll end up with words like "at\n" in your histogram.
while (defined (my $line = <STDIN> )) { chomp $line; my @currentline = split /\s+/, $line; $histogram{$_}++ for @currentline; }
Another basic issue, why do you iterate over your histogram hash? This sort of thing is exactly what hashes are designed to allow you to avoid. Just test for existence, and retrieve the value as needed. I
if( exists $histogram{$word} ) { print "Found $word, $histogram{$word} times\n"; }
Update: ysth is right. The chomp is not needed. split seems to be especially hungry. I was expecting it to match like the code below.
use Data::Dumper; __END__ #Result: while ( <STDIN> ) { my @foo = /(\s+)/; print Dumper \@foo; } C:\> test.pl cat food $VAR1 = [ ' ' ];
TGI says moo
In reply to Re: 'Simple' comparing a hash with an array
by TGI
in thread 'Simple' comparing a hash with an array
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |