Dear Monks,
I am struggling with finding the cause of my code's strange output.
I am trying to find a list of words and count their frequency in a text file given through STDIN. So far, I hoped that the code would print the hash's key/value pairs that matched my array's elements.
It appears to output some key/values and not others using a logic I can't follow. I'm sure I'm commiting a common mistake for this bread-and-butter Perl problem.
Anonymous Newbie
#!/usr/bin/perl -w use strict; my %histogram; my @wordstofind = qw( a am an and are as at be been but by can co de do due each ); #Many more words snipped while (<STDIN>) { my @currentline = split; $histogram{$_}++ for @currentline; } foreach my $i (@wordstofind) { while ( my ($key, $value) = each (%histogram) ) { if ($key =~ /$i/) { print "Found $key, $value times\n"; } } }
In reply to 'Simple' comparing a hash with an array by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |