Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: arrays: shifting in while loop

by AnomalousMonk (Archbishop)
on Mar 06, 2012 at 21:36 UTC ( [id://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.

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://958166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-19 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found