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
Nodejs package.json resolutions How to find Operating System username in NodeJS? How to convert Double to Integer or Integer to double in Dart| Flutter By Example Ways to skip test case execution in Gradle project build Learn Gradle | tutorials, and examplesRelated posts