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.

— Ken


In reply to Re: Next from inner loop only works "most of the time" by kcott
in thread Next from inner loop only works "most of the time" by Marshall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.