How to break from a do while loop in perl?

Discussion in 'Perl Misc' started by jeniffer, Mar 28, 2006.

  1. jeniffer

    jeniffer Guest

    i read that last doesnt work inside a do while loop.Then how to break
    out from a do-while loop...
    do{
    if(some condition)
    {
    last;
    }

    }while(condition)
    jeniffer, Mar 28, 2006
    #1
    1. Advertising

  2. jeniffer

    Matt Garrish Guest

    "jeniffer" <> wrote in message
    news:...
    >i read that last doesnt work inside a do while loop.Then how to break
    > out from a do-while loop...
    > do{
    > if(some condition)
    > {
    > last;
    > }
    >
    > }while(condition)
    >


    Do what the docs say: wrap it in a labeled block (see perlsyn, "Statement
    Modifiers").

    Depending on how simple your loop is, you could also just set the while
    condition to true in the if conditional and run your code in the else.

    Matt
    Matt Garrish, Mar 28, 2006
    #2
    1. Advertising

  3. jeniffer wrote:
    > i read that last doesnt work inside a do while loop.Then how to break
    > out from a do-while loop...
    > do{
    > if(some condition)
    > {
    > last;
    > }
    >
    > }while(condition)


    What you want to do is described in the "Statement Modifiers" section of the
    perlsyn document.

    perldoc perlsyn



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Mar 28, 2006
    #3
  4. jeniffer

    jeniffer Guest

    John W. Krahn wrote:
    > jeniffer wrote:
    > > i read that last doesnt work inside a do while loop.Then how to break
    > > out from a do-while loop...
    > > do{
    > > if(some condition)
    > > {
    > > last;
    > > }
    > >
    > > }while(condition)

    >
    > What you want to do is described in the "Statement Modifiers" section of the
    > perlsyn document.
    >
    > perldoc perlsyn
    >
    >
    >
    > John
    > --
    > use Perl;
    > program
    > fulfillment




    I did the following:

    LOOP: {
    do {
    last if (condition)
    {
    print "exit message";
    }
    # do something here
    } while (condition2);
    }


    but again it does not work
    jeniffer, Mar 28, 2006
    #4
  5. "jeniffer" <> wrote in
    news::

    >
    > John W. Krahn wrote:
    >> jeniffer wrote:
    >> > i read that last doesnt work inside a do while loop.Then how to
    >> > break out from a do-while loop...
    >> > do{
    >> > if(some condition)
    >> > {
    >> > last;
    >> > }
    >> >
    >> > }while(condition)

    >>
    >> What you want to do is described in the "Statement Modifiers" section
    >> of the perlsyn document.
    >>
    >> perldoc perlsyn


    snip quoted sig ... don't do that.

    > I did the following:
    >
    > LOOP: {
    > do {
    > last if (condition)
    > {
    > print "exit message";
    > }
    > # do something here
    > } while (condition2);
    > }
    >
    >
    > but again it does not work


    Post a short but complete script that demonstrates the problem. When
    posting code, use proper indentation to make it easier for others to
    read your code.

    D:\Home\asu1\UseNet\clpmisc> cat brk.pl
    #!/usr/bin/perl

    use strict;
    use warnings;

    my $x = 10;

    LOOP: {
    do {
    --$x;
    last LOOP if 0.5 < rand 1;
    } while ( $x );
    }

    print "\$x = $x\n";


    D:\Home\asu1\UseNet\clpmisc> brk
    $x = 7

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Mar 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,225
    Tim Slattery
    Mar 30, 2005
  2. Replies:
    16
    Views:
    3,574
    steve
    Jul 11, 2006
  3. Claudio Grondi
    Replies:
    16
    Views:
    475
    Matimus
    Aug 30, 2006
  4. Uday Bidkar
    Replies:
    4
    Views:
    484
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page