G'day Marshall,
I added strict and warnings: both missing from your code.
I also show assignments to %master_letter_freq and @result: also missing from your code.
The remainder is a verbatim copy of the code you posted.
#!/usr/bin/env perl
use strict;
use warnings;
my %master_letter_freq = qw{w 1 h 1 t 1 o 1 r 2 e 1};
my @result = qw{thew trow whew};
RESULT: foreach (@result)
{
my %seen;
$seen{$_}++ for (split //,lc $_);
foreach (keys %seen)
{
next RESULT if ($seen{$_} > $master_letter_freq{$_});
}
print "$_\n";
}
foreach (@result)
{
my %seen;
$seen{$_}++ for (split //,lc $_);
my $no_print = 0;
foreach (keys %seen)
{
$no_print++ if ($seen{$_} > $master_letter_freq{$_});
}
print "$_\n" unless $no_print;
}
Here's the output:
thew
trow
thew
trow
Please run that code, exactly as is, and report the output.
If you still see "whew" in the output, then there's potentially a bug in Perl.
I'm using 5.32.0. What are you using?
Post an SSCCE so we can investigate.
If you don't see "whew" in the output, then either the code you're running is not the code you posted,
or there's something else going on in the code before this that you haven't shown.
Add code to query every variable before and/or after it changes.
For instance, with Data::Dump:
dd \%master_letter_freq; # <-- QUERY
dd \@result; # <-- QUERY
RESULT: foreach (@result)
{
print "|$_|\n"; # <-- QUERY
my %seen;
$seen{$_}++ for (split //,lc $_);
dd \%seen; # <-- QUERY
# ... and so on ...
Note the pipe characters (|$_|) that may help to identify whitespace or control characters that aren't readily visible.
If that doesn't help, try (platform-dependent):
./script.pl | cat -vet
That's something of a last resort, but it may show up something if all else has failed to identify the problem.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.