Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Is it safe to append to the array you are iterating over

by BrowserUk (Pope)
on Mar 14, 2013 at 19:07 UTC ( #1023532=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Is it safe to append to the array you are iterating over
in thread Is it safe to append to the array you are iterating over

The problem is that even if the logic in the loop body doesn't cause an infinite loop; you are at the very least also iterating over all the additional elements that you push for no good reason. You've just pushed those values; you presumably don't want them to cause further pushes.

Why do pointless extra work when it is so easily avoided?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.
/div


Comment on Re^3: Is it safe to append to the array you are iterating over
Re^4: Is it safe to append to the array you are iterating over
by DrWhy (Chaplain) on Mar 14, 2013 at 19:23 UTC
    I disagree with your 'for no good reason' part. In my use case, I have a very good reason to want this to happen. (I mean the real code I'm working on, not the baby example I used in the OP). I'm using it to traverse a tree of nodes and build a list (actually a hash) of data about the nodes.

    But as I said in another comment, I'm switching to using an implementation that uses a while loop instead that's clearly safer.

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

      So reject the idea that there are better ways to do it; but decided to change the way you do it anyway. That's ...


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

        The simplest and most straightforward way is to append to the array and let it run until it is done. However unlikely it is to be broken, there is no guarantee that will continue to work.

        A safer solution was chosen instead. A fairly small reduction in simplicity for the warm fuzzies of safety.

        Which one is "better" is at this point matter of opinion. In one sense the final choice was deemed best because it was the final choice. In another sense, the original was best (aside from being disqualified on a technicality). In the same sense that a runner can be fastest but not win.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-09-17 18:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (94 votes), past polls