Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: arrays: shifting in while loop

by AnomalousMonk (Abbot)
on Mar 06, 2012 at 21:36 UTC ( #958166=note: print w/ replies, xml ) Need Help??


in reply to arrays: shifting in while loop

My understanding is that the while will execute so long as there are elements in @a...

ivanthemad: I expect you already understand the following point perfectly well, but I want to dispell my sneaking suspicion of a possible misapprehension on your part.

The while-loop in the example code will only begin execution (Update: of a loopthe loop body) if the array is non-empty. However, (Update: once begun) looploop body execution will continue regardless of the state of the array unless an explicit loop exit is made conditional upon array state, e.g., with a statement like  last unless @a;.

Update: Clarified (hopefully) pertinence to loop body per repellent.


Comment on Re: arrays: shifting in while loop
Select or Download Code
Re^2: arrays: shifting in while loop
by repellent (Priest) on Mar 07, 2012 at 05:01 UTC
      ... loop execution will continue regardless of the state of the array unless an explicit loop exit is made ...

    Hmm, really? I always thought the while-loop EXPR was re-evaluated after each iteration:
    my @a = (3, 4); while (do { say "yo"; @a }) { say "hi mom"; shift @a; # no explicit loop break out } say "bye mom"; __END__ yo hi mom yo hi mom yo bye mom
      ... the while-loop EXPR [is] re-evaluated after each iteration...

      It is, but I was trying to make a (perhaps rather trivial) point with regard to continued execution of the loop body (which I've tried to clarify in the original reply) as exemplified below.

      >perl -wMstrict -le "my @ra = (9, 8, 7); ;; while (@ra) { print 0+@ra, ' in @ra'; shift @ra; print 0+@ra, ' in @ra'; shift @ra; print 0+@ra, ' in @ra'; shift @ra; print 0+@ra, ' in @ra'; shift @ra; print 0+@ra, ' in @ra'; shift @ra; print 0+@ra, ' in @ra'; } " 3 in @ra 2 in @ra 1 in @ra 0 in @ra 0 in @ra 0 in @ra
        Great, I knew I missed something from your original post.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-07-04 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls