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

Re^2: self-feeding infinite loop

by spx2 (Deacon)
on Aug 18, 2007 at 14:03 UTC ( [id://633482]=note: print w/replies, xml ) Need Help??


in reply to Re: self-feeding infinite loop
in thread self-feeding infinite loop

on the contrary,the code i first posted , namely the first example is showing that using foreach and push works to achieve an infinite loop behaviour.

Replies are listed 'Best First'.
Re^3: self-feeding infinite loop
by bart (Canon) on Aug 18, 2007 at 14:17 UTC
    Maybe it works, maybe it doesn't. But you're definitely in the territory of "unpredictable behaviour".
      But you're definitely in the territory of "unpredictable behaviour"

      I may be being somewhat picky (or I may even be downright wrong), but I don't see any "unpredictable behaviour" there. I agree that one needs to be careful when modifying an array by iterating over the same array, but the following does behave predictably:
      use strict; use warnings; my @array = ('a'); foreach (@array) { push @array,'a'; print"@array\n"; sleep(1); # so we can absorb what's happening }
      Cheers,
      Rob
        It's not "unpredictable" in the sense of being random but it is undefined in the sense that it is not specified in the documentation and could potentially change in a future release. Quoting perlsyn:
        If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that.
        I should add for the record that I once asked the same question myself.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://633482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found