How to break from a do while loop in perl?

J

jeniffer

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)
 
M

Matt Garrish

jeniffer said:
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
 
J

John W. Krahn

jeniffer said:
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
 
J

jeniffer

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

perldoc perlsyn



John



I did the following:

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


but again it does not work
 
A

A. Sinan Unur

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top