Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: self-feeding infinite loop

by Dominus (Parson)
on Aug 20, 2007 at 14:02 UTC ( #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.


Comment on Re^2: self-feeding infinite loop
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
Node Status?
node history
Node Type: note [id://633826]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-09-16 14:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (31 votes), past polls