How to use next and last in Perl
2005-11-22 15:24
483 查看
next skips to the next evaluation of the loopWhen you use next in Perl, you skip to the next evaluation of the current loop. In other words, if the loop is nested within another control structure, next only affects the loop which contains it.
my $counter;
my $test_var;
for( $counter = 0; $counter < 3; $counter++)
{
print "counter: $counter/n";
foreach $test_var (1,2,3,1,2,3) # Point A
{
if( $test_var == 2 )
{
next; # Jumps to Point A
}
print "$test_var ";
}
print "/n";
}
print "DONE!";
OUTPUT:
counter: 0
1 3 1 3
counter: 1
1 3 1 3
counter: 2
1 3 1 3
DONE!
As you can see, we iterate three times. Within that loop, Perl prints the values for $test_var in the foreach loop. Normally that means it should print 1 2 3 1 2 3. However, when $test_var is equal to two, Perl hits the next statement and skips to the loop at Point A, and thus it never gets printed. The foreach loop continues as it normally would, so the remaining values get printed.
If you need to have Perl jump to a specific control structure with next, you can label it. Let's say that when Perl hits the next statement, we'd like it to jump to the for loop, which we'll call Point B. Here's how we'd modify the Perl code.
POINT_B: for( $counter = 0; $counter < 3; $counter++)
{
print "counter: $counter/n";
foreach $test_var (1,2,3,1,2,3) # Point A
{
if( $test_var == 2 )
{
next POINT_B;
}
print "$test_var ";
}
print "/n";
}
OUTPUT:
counter: 0
1 counter: 1
1 counter: 2
1
Now every time Perl hits next in the foreach loop, it jumps all the way out to the for loop, which is now labeled POINT_B. Both the print statements and the newline don't get printed, and that's why the results look so different.
Use last to break out of the loop entirely
Unlike next, Perl does no further evaluation when you use last. Instead Perl jumps to the first executable line of code outside of the loop. The syntax looks the same as it does for next. However, the results are quite different.
for( $counter = 0; $counter < 3; $counter++)
{
print "counter: $counter/n";
foreach $test_var (1,2,3,1,2,3) # Point A
{
if( $test_var == 2 )
{
last;
}
print "$test_var ";
}
print "/n";
}
OUTPUT:
counter: 0
1
counter: 1
1
counter: 2
1
DONE!
It's similar to using next with labels, as in the example above, but this time Perl prints the newline (/n). Why? Because it's the next line of code that follows foreach. In the example with next, Perl jumped to the label POINT_B without executing any more code, so the newline was skipped. This time Perl just quit the foreach loop, and continued with the rest of the code as it normally would. You can also use labels with last, but I'll leave that as an exercise for you to do.
my $counter;
my $test_var;
for( $counter = 0; $counter < 3; $counter++)
{
print "counter: $counter/n";
foreach $test_var (1,2,3,1,2,3) # Point A
{
if( $test_var == 2 )
{
next; # Jumps to Point A
}
print "$test_var ";
}
print "/n";
}
print "DONE!";
OUTPUT:
counter: 0
1 3 1 3
counter: 1
1 3 1 3
counter: 2
1 3 1 3
DONE!
As you can see, we iterate three times. Within that loop, Perl prints the values for $test_var in the foreach loop. Normally that means it should print 1 2 3 1 2 3. However, when $test_var is equal to two, Perl hits the next statement and skips to the loop at Point A, and thus it never gets printed. The foreach loop continues as it normally would, so the remaining values get printed.
If you need to have Perl jump to a specific control structure with next, you can label it. Let's say that when Perl hits the next statement, we'd like it to jump to the for loop, which we'll call Point B. Here's how we'd modify the Perl code.
POINT_B: for( $counter = 0; $counter < 3; $counter++)
{
print "counter: $counter/n";
foreach $test_var (1,2,3,1,2,3) # Point A
{
if( $test_var == 2 )
{
next POINT_B;
}
print "$test_var ";
}
print "/n";
}
OUTPUT:
counter: 0
1 counter: 1
1 counter: 2
1
Now every time Perl hits next in the foreach loop, it jumps all the way out to the for loop, which is now labeled POINT_B. Both the print statements and the newline don't get printed, and that's why the results look so different.
Use last to break out of the loop entirely
Unlike next, Perl does no further evaluation when you use last. Instead Perl jumps to the first executable line of code outside of the loop. The syntax looks the same as it does for next. However, the results are quite different.
for( $counter = 0; $counter < 3; $counter++)
{
print "counter: $counter/n";
foreach $test_var (1,2,3,1,2,3) # Point A
{
if( $test_var == 2 )
{
last;
}
print "$test_var ";
}
print "/n";
}
OUTPUT:
counter: 0
1
counter: 1
1
counter: 2
1
DONE!
It's similar to using next with labels, as in the example above, but this time Perl prints the newline (/n). Why? Because it's the next line of code that follows foreach. In the example with next, Perl jumped to the label POINT_B without executing any more code, so the newline was skipped. This time Perl just quit the foreach loop, and continued with the rest of the code as it normally would. You can also use labels with last, but I'll leave that as an exercise for you to do.
while loop and continue in Perl
There is another form of the while loop in Perl, and this one has a continue block on it. A continue block is a block of code that is executed after the loop is finished, but before the loop is evaluated for the next iteration.my $test = 0; $counter = 0; while($counter++ < 5) # Point A { if($counter == 3) { $test = 1; next; } print "While Loop: $counter/n"; } continue # Point B { if($test == 1) { print "CONTINUE: $counter /n"; $test = 0; } } print "DONE!"; # Point CWhen Perl finishes the code within the block, it proceeds to the continue block at Point B, rather than returning directly to Point A as it does in the examples above. There's a bit of a twist in the example, because I've introduced the keyword next, which will be discussed in detail later in this feature. However, a quick explanation is that next is used to break out of a loop.######################################################################附注:C++ Syntax: continue
Description
continue may only be used inside a loop, such as a for statement. It terminates the current iteration of the loop and proceeds directly to the next. In the case of a for loop it jumps to its increment-expr. Where loop statements are nested, continue terminates the current iteration of the innermost one loop containing it.Description
continue may only be used inside a loop, such as a for statement. It terminates the current iteration of the loop and proceeds directly to the next. In the case of a for loop it jumps to its increment-expr. Where loop statements are nested, continue terminates the current iteration of the innermost one loop containing it.Description
continue may only be used inside a loop, such as a for statement. It terminates the current iteration of the loop and proceeds directly to the next. In the case of a for loop it jumps to its increment-expr. Where loop statements are nested, continue terminates the current iteration of the innermost one loop containing it.相关文章推荐
- [Mootools] Simple tutorial of how to use domReady, DOM operation and Ajax in Mootools
- c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow
- How to create a DLL library in C and then use it with C#
- How to use *args and **kwargs in Python
- how to save a c++ object in java object and use it
- use noscript html tag when user disable the javascript in browser, guide user how to enable the js in different browser and retu
- How to Reference and Use JSTL in your Web Application
- How and Why to use the System.servicemodel.MessageParameterAttribute in WCF
- How to use, monitor, and disable transparent hugepages in Red Hat Enterprise Linux 6
- How to use equals( ) and equalsIgnoreCase( ) in Java
- How to Setup and Use Github in Ubuntu
- How to use Pageheap.exe in Windows XP, Windows 2000, and Windows Server 2003
- How to use composition and inheritance in visual c# ?
- How to use USB 3G dongle/stick Huawei E169/E620/E800 ( Chip used Qualcomm e1750) in Linux (China and world)
- ACC97: How to Use Percent, Plus, and Caret Symbols in ASP Forms
- How to use $http and $resource in Angular JS
- (转贴)How to use JDBC and transactions in Tomcat with JOTM
- [zz]How to use *args and **kwargs in Python
- Know How And When To Use System.Message_Level To Control Messages In Oracle Forms
- How to use plugin audio codecs in OpenH323 and OPAL