Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: self-feeding infinite loop

by Dominus (Parson)
on Aug 20, 2007 at 14:02 UTC ( [id://633826]=note: print w/replies, xml ) Need Help??


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

It's not clear to me exactly what you're trying to do, but you've sinned against a big no-no:
Do not modify an array in a foreach loop over the same array.
I argued a few years ago on p5p that the prohibition in the manual is excessive, since the behavior is actually fairly simple and reliable. The only real problem with the technique is that, since p5p has declined to guarantee the current simple, reliable behavior, you have to worry that perhaps a future version of Perl will change the behavior.

I tried to get p5p to agree to guarantee the current behavior, but it didn't happen.

In former times there was a similar warning about modifying a hash in the middle of a while (each %hash) loop, even though the hash code had a special case in it to handle that exact use case, since version 5.000. The prohibition in the manual had been added later by someone with more superstition and less understanding of the actual behavior.

A lot of stuff in the manual is like that, warning you that you must throw salt over your shoulder to avoid the wrath of the Moon God, or whatever.

I don't think referring to warnings in the manual as "no-nos" or thinking of them in terms of "sin" is a very effective way to handle this sort of issue. We are supposed to be professional adults, not toddlers, and this is supposed to be an engineering discipline, not some mystical rite. Using the language of superstition can only promote superstitious behavior.

Replies are listed 'Best First'.
Re^3: self-feeding infinite loop
by eric256 (Parson) on Aug 20, 2007 at 16:33 UTC

    So if you splice an array while using for what do you expect it to do? Especially if you splice elements such that your current index either no longer exists or is in a completly new location? More importantly how many people can agree on what the correct behavior is then?


    ___________
    Eric Hodges
      your current index either no longer exists or is in a completly new location?
      The index is a number. Numbers do not have locations, and cannot be created or destroyed, not even by the splice operator.

        s/index/item being references by your index/

        The point wasn't about indexes being numbers or not, it was that modifying an array while iterating over it is dangerous and can lead to cases where it isn't obvious what should be done. In addition useing for or foreach isn't using indexes in an obvious manner so that when using it we seldom think about it as just using an index and counting up to the end of the array. Instead we (me at least) perceive it as moving over the actual values so that if you add or remove values I have no clear idea of what that should do to a for except I certainly expect it to break.


        ___________
        Eric Hodges

Log In?
Username:
Password:

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

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

    No recent polls found