Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

last in a do while loop

by gam3 (Curate)
on Nov 03, 2006 at 13:00 UTC ( [id://582069]=perlquestion: print w/replies, xml ) Need Help??

gam3 has asked for the wisdom of the Perl Monks concerning the following question:

Today I had a while loop that really needed to be a do while loop.
while (alice) { charlie; last if bob; david; }
where alice, bob, charlie and david are apropriate subroutines
Doing a simple transformation to
do { charlie; last if bob; david; } while (alice);
does not compile, since the do does not know anything about the while.

There are certainly lots of ways to fix this, but I thought that there might be an eligant approach that I have not though of.
-- gam3
A picture is worth a thousand words, but takes 200K.

Replies are listed 'Best First'.
Re: last in a do while loop
by Hofmator (Curate) on Nov 03, 2006 at 13:23 UTC
    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.

      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.

Re: last in a do while loop
by davorg (Chancellor) on Nov 03, 2006 at 13:28 UTC

    See the section on statement modifiers in perlsyn.

    Note also that the loop control statements described later will NOT work in this construct, because modifiers don't take loop labels. Sorry. You can always put another block inside of it (for next) or around it (for last) to do that sort of thing. For next, just double the braces:

    do {{ next if $x == $y; # do something here }} until $x++ > $z;

    For last, you have to be more elaborate:

    LOOP: { do { last if $x = $y**2; # do something here } while $x++ <= $z; }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Mmhh, I know this is just copy and pasted code, but it makes me wonder anyway - is it just me or is the code for the last example slightly strange?
      LOOP: { do { last if $x = $y**2; # do something here } while $x++ <= $z; }
      • if $x = $y**2; why choose a confusing assignment instead of a normal comparison?
      • Why not the explicit last LOOP? I know that it works here without it all right but I don't see the benefit of labelling the block without using the label afterwards

      -- 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.

        I was thinking exactly the same things as I was writing the post. I foresee a couple of patches being sent to the maintainers.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found