Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Program enters infinite loop, can you see why?

by BrowserUk (Patriarch)
on Dec 07, 2010 at 03:07 UTC ( [id://875722]=perlmeditation: print w/replies, xml ) Need Help??

It took me a long time to see the cause of this, (but it was processing 1 GB of data), and I thought it might prove interesting/amusing to others.

Greatly simplified from the original that was processing data from a file, this program never terminates. Can you see why?

#! perl -slw use strict; my @bits = unpack '(a3)*', 'abcdefghijklmnopqrstuvwxyz'; 1 while "@bits[3,5,7]" =~ m[vwx]g;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: Program enters infinite loop, can you see why?
by ikegami (Patriarch) on Dec 07, 2010 at 03:58 UTC
      (Ray's lightbulb comes on.) Thanks Ikegami! This explanation makes best sense.
Re: Program enters infinite loop, can you see why?
by JavaFan (Canon) on Dec 07, 2010 at 10:09 UTC
    It's one of the cases where quoting your variables can have an unexpected effect:
    $x = "abc"; say 1 while $x =~ /c/g; say 2 while "$x" =~ /c/g;
    will print a single 1, and an unlimited number of 2s.
Re: Program enters infinite loop, can you see why?
by Anonymous Monk on Dec 07, 2010 at 03:56 UTC

      By that logic, this should also hang, but it doesn't:

      C:\test>perl -wE"1 while 'vwx' =~ m[vwx]g;"

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        interpolation always interpolates
Re: Program enters infinite loop, can you see why?
by pajout (Curate) on Dec 07, 2010 at 10:14 UTC
    The condition of the cycle is NOT changed due one loop...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://875722]
Approved by Arunbear
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found