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

Updated:   |  Perl (Scripting) articles

Typically, one of the following is used to break out of a loop or to move onto the next item in a loop.

  • last (break out of a loop)
  • next (move onto next item in a loop)
  • return (break out of a subroutine)

 

 

next 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 my $value (@array) {
  print "$value\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 my $value (@array) {

  if ($value =~ /two/i) {
    next;
  }
  print "$value\n";
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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