Monday, July 18, 2011

last keyword perl

The last keyword is used if you want to break out of the statement block.
This can be used in cases when you iterate through the variables in a array,when you
found the value you want to break out of the loop.


Example:

@array=(1..10);
foreach $var(@array)
{
last if($var==5);
print "The value of var is $var"."\n";
}

OUTPUT:

G:\perl_programs>perl perl_last.pl
The value of var is 1
The value of var is 2
The value of var is 3
The value of var is 4

G:\perl_programs>
Here as you may see from the program it breaks the loop when the value of the $var reaches 5.

No comments:

Post a Comment