THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial explains multiple ways to break and exit from a loop in Perl loops.
Many programming languages provide break keywords to exit from a loop. Perl has the last to exit from the loop.
Perl provides multiple ways to loop iteration using for, foreach, while, and do while syntax.
Here is foreach last example
my @numbers=("one","two","three");
for my $item (@numbers){
if ($item eq "three"){
last;
}
print "$item,\n"
}
Here is foreach last example
my @numbers=("one","two","three");
foreach my $item (@numbers){
if ($item eq "three"){
last;
}
print "$item,\n"
}
Here is while last example
my @numbers=("one","two","three");
my $counter=0;
my [email protected]; ## get size
while($counter <$size){
if (@numbers[$counter] eq "three"){
last;
}
print "@numbers[$counter],\n";
$counter=$counter+1;
}
Here is while last example
my @numbers=("one","two","three");
my $counter=0;
my [email protected]; ## get size
do {
if (@numbers[$counter] eq "three"){
last;
}
print "@numbers[$counter],\n";
$counter=$counter+1;
} while($counter <$size);
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts