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

by
Jeremy Canfield |
Updated: December 01 2021
| 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.
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