Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I wrote some code today that worked most of the time but not all of the time -> different runs of Perl produced different results.
I have fixed the code so that it is reliable. My question is "what happens inside Perl such that my first version was unreliable?"

I started playing a scrabble like game on my cellphone and once I got to level 108, things got hard. So I whipped out a cheater program! Pretty easy to do and my quick hack worked fine except that it made mistakes but only some of the time.

Here is the output of a "good" run:

list of letters or pattern: :otrwreh master_letter_freq: $VAR1 = { 'w' => 1, 'h' => 1, 't' => 1, 'o' => 1, 'r' => 2, 'e' => 1 }; list of letters or pattern: ---w thew trow list of letters or pattern:
The letters "otrwreh" are its "vocabulary" for forming words. "r" can be repeated, but none of the other letters.
The pattern "---w", means show all 4 letter words in the dictionary that end in "w" that are formed using only the letters "otrweh".

All well and good until I saw this run:

list of letters or pattern: :otrwreh list of letters or pattern: ---w thew trow whew ## this is the problem!! "w" is not allowed to repeat! list of letters or pattern: exit
Ok, the offending unreliable code snippet:
RESULT: foreach (@result) { my %seen; $seen{$_}++ for (split //,lc $_); foreach (keys %seen) { next RESULT if ($seen{$_} > $master_letter_freq{$_}); } print "$_\n"; }
Once the regex does its thing on a huge list of possible words, I take out any results where a letter occurs more often in the result than in the pattern list of characters. so, "whew" should get thrown away. But evidently the next is not being executed. Sometimes the code falls through the loop and "whew" gets printed even though "w" occurs too often for the "rules".

Now arguably using "next" in this way is not the brightest thing I've done. But when hacking, stuff happens and sometimes I try something new with Perl. This was just "throw away" code for my own amusement.

Of course I rewrote the code using a more traditional approach like this:

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; }
So, I get the answer of "hey, don't do that!". I am curious why my hack was unreliable? It worked often enough that I figured that it was ok, until problems showed up later on. I had never used next in this way, although I have used a labeled redo before.

Update: Now that I think about it, it could be that using the default variable $_ in both loops could be an issue. I normally would assign an explicit my variable for the loop variable. But in quick, just barf if out code, that didn't happen here.

UPDATE
I think tybalt89 came up "with the ball" at Re^3: Next from inner loop only works "most of the time".


In reply to 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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found