现在的位置: 首页 > 综合 > 正文

How to use next and last in Perl

2013年03月21日 ⁄ 综合 ⁄ 共 3934字 ⁄ 字号 评论关闭
文章目录

next skips to the next evaluation of the loop

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


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 C

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

抱歉!评论已关闭.