Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: last in a do while loop

by Hofmator (Curate)
on Nov 03, 2006 at 13:23 UTC ( [id://582072]=note: print w/replies, xml ) Need Help??


in reply to last in a do while loop

does not compile, since the do does not know anything about the while.
~/perl$ cat x do { charlie; last if bob; david; } while (alice); ~/perl$ perl -c x x syntax OK ~/perl$ perl -v This is perl, v5.6.1 built for i386-linux
So it compiles just fine.

What is not working, though, is the last. This is because the block introduced with do is neither a loop nor a bare block. Your statement is basically equivalent in structure to print foo while bar.

What you can do instead is a bare block, because you can use next, last, redo on a bare block:

LOOP: { charlie; last LOOP if bob; david; redo LOOP if alice; }

-- Hofmator

Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: last in a do while loop
by ikegami (Patriarch) on Nov 03, 2006 at 15:48 UTC
    LOOP: { charlie; last LOOP if bob; david; redo LOOP if alice; }

    A more concise syntax:

    for (;;) { charlie; last if bob; david; last if !alice; }

    It's my understanding that for (;;) { ... } can be replaced simply with loop { ... } in Perl6.

        There's one significant advantage (from the reader's point of view) to while (1):

        • The meaning of while (1) is straightforward to deduce by someone who has never seen it, whereas the meaning of for (;;) is not.

        There are minor advantages (from the reader's point of view) to for (;;):

        • for (;;) has no expression to read, while while (1) does. (Note the addition of loop { ... } in Perl 6.)

        • "(;;)" can be read as "ever". "For ever" sounds better than "while one", "while true" or "while ever" (and requires no mental backtracking). "While not done" would be a great reading, but translating "1" to "not done" is a stretch.

        • for (;;) is visually distinctive from naturally ending loops.

        Update: hum... while (!0) could be read as "while not done"...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-28 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found