Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: self-feeding infinite loop

by eric256 (Parson)
on Aug 20, 2007 at 16:33 UTC ( #633863=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: self-feeding infinite loop
by Dominus (Parson) on Aug 20, 2007 at 16:50 UTC
    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
        The point wasn't about indexes being numbers or not...
        Yes, I understood that you had missed this essential point. That was why I tried to bring it to your attention.

        it was that modifying an array while iterating over it is dangerous
        As I tried to suggest in my original message, it isn't dangerous. Sorry this wasn't clear to you.

        I understand that you fear this construction because you don't understand it, and that you have a faulty mental model of the way foreach loops are processed, which leads you to an incorrect expectation of what they will do when the underlying array is modified. However, this fear is only your private emotion. It is real, but it does not extend outside of yourself. In particular, you cannot conclude from the fact of your fear that the practice is actually "dangerous".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2021-12-04 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?