Syntax:
The syntax of the Perl “next” operator is shown in the following:
Or
This operator can be used with and without a label. The simple “next” operator is used to go to the next iteration of the loop by skipping any statement of the loop. The “next” operator with the label is used to jump into the particular statement of the script.
Different Examples of the Perl “Next” Operator
Different uses of the “next” operator are shown in this part of the tutorial.
Example 1: Using the “Next” Operator in the “While” Loop without a Label
Create a Perl file with the following script where the “next” operator is used to iterate the “while” loop. Here, the infinite “while” loop is declared and a counter variable is used to terminate the loop. The loop is iterated using the “next” operator until the counter value is equal to 9. In each iteration, each odd number is added with the $sum variable and the sum of all odd numbers from 1 to 10 is printed after terminating from the loop.
use strict;
use warnings;
use 5.34.0;
#Initialize the counter
my $counter = 1;
#Initialize the sum variable
my $sum = 0;
#Set the variable to start the infinite while loop
my $valid = 1;
say "All odd numbers from 1 to 10 are:";
while ($valid)
{
#Print the odd number and add to the $sum
if ($counter % 2 > 0)
{
print "$counter ";
$sum = $sum + $counter;
}
#Check the current $counter value to terminate the loop
if ($counter > 9)
{
print "\n";
$valid = 0;
say "The sum of all odd numbers from 1 to 10 is $sum";
}
#Increment the counter by one
$counter++;
#Go to the next iteration
next;
}
Output:
The following output appears after executing the previous script. The odd numbers from 1 to 10 are 1, 3, 5, 7, and 9. The sum of these numbers is 25 which is printed in the output:
Example 2: Using the “Next” Operator in the “For” Loop with a Label
Create a Perl file with the following script where the “next” operator is used to iterate the “for” loop with a label. The nested “for” loop is used in the script and the label is defined before the inner “for” loop. The “next” operator with the label is used to skip the “print” statement for some conditions.
use strict;
use warnings;
use 5.34.0;
#Outer for loop
for (my $team = 1; $team <=2; $team++)
{
say "The following members of team $team are present:";
member:
#Inner for loop
for (my $member = 1; $member <=11; $member++)
{
#Omit the members 5 and 8 for team 1
if ($team == 1 && ($member == 5 || $member == 8))
{
next member;
}
#Omit the members 3, 5, and 7 for the team 2
if ($team == 2 && ($member == 3 || $member == 5 || $member == 7))
{
next member;
}
#Print the members who are present
print "$member ";
}
#Add newline
print "\n";
}
Output:
The following output appears after executing the previous script. Two members, 5 and 8, of team 1 and three members which are 3, 5, and 7 are omitted from the output:
Example 3: Using the “Next” Operator with the Conditional Statement
Create a Perl file with the following script where the “next” operator is used to iterate the “while” loop to read the content of a file named “fruits.txt”. Here, the “next” operator is used to filter some content of the file.
use strict;
use warnings;
use 5.34.0;
#Open a file for reading
open(File_handler, "< fruits.txt");
say "The filtered content of the file:";
#Read the file content line by line
while(<File_handler>)
{
my $line = $_;
chomp($line);
#Omit the line if the following condition returns true
if ($line eq "Mango" || $line eq "Guava")
{
next;
}
else
{
print "$line ";
}
}
#Add newline
print "\n";
The following output appears after executing the previous script. In the output, two lines of the file that contain “Mango” and “Guava” are omitted from the output:
Conclusion
The uses of the “next” operator for multiple purposes are shown in this tutorial using multiple examples to help the Perl users to know the use of the “next” operator.