FreeKB - Moving onto next value in a loop in Perl (next)
Moving onto next value in a loop in Perl (next)

Home > Search
  by

The next operator is used to move onto the next value in a loop. For example, consider the following script. This script will print the text one two three four to the console.

#!/usr/bin/perl

use strict;
use warnings;

my @array = qw(one two three four);

foreach (@array) {
  print "$_\n";
}

 

In this example, if two is detected, the loop will move onto the next value. In this example, text one three four will be printed to the console.

#!/usr/bin/perl

use strict;
use warnings;

my @array = qw(one two three four);

foreach (@array) {

  if ($_ =~ "two") {
    next;
  }
  print "$_\n";
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments