Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to break from a do while loop in perl?

Reply
Thread Tools

How to break from a do while loop in perl?

 
 
jeniffer
Guest
Posts: n/a
 
      03-28-2006
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)

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      03-28-2006

"jeniffer" <> wrote in message
news: oups.com...
>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


 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      03-28-2006
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
 
Reply With Quote
 
jeniffer
Guest
Posts: n/a
 
      03-28-2006

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

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-28-2006
"jeniffer" <> wrote in
news: oups.com:

>
> 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/cl...uidelines.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
How to break a while loop inside a switch statement? howachen@gmail.com Java 16 07-11-2006 10:23 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57