Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Why does a full iteration over array & shift is not emptying the array

by moritz (Cardinal)
on May 04, 2010 at 13:39 UTC ( [id://838308]=note: print w/replies, xml ) Need Help??


in reply to Why does a full iteration over array & shift is not emptying the array

Modifying the array you're iterating over is a bad idea.

The explanation here is that $x is bound to the first array item (1) in the first iteration, and to the second array item (which is then 3) in the second iteration. At that time there's no third item in the array anymore, and the body of the loop is executed only twice (as you can see from your debugging output).

It becomes a bit clearer if you also print out $x in the loop body.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-19 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found