M
Michael
I am new to perl, and I am wondering whether it is a bad thing to jump out
of a foreach loop with return. I have a function which searches for a
value in one of several arrays. If the value is found in any of them,
then the whole function may as well terminate. At the moment I am setting
a flag when the value is found, which stops execution of the later loops.
Would it be safe / stylistically sound to do away with the flag by using
"return" rather than "last". Example code follows.
TIA
sub example{
my $found;
$found = 0;
foreach $item (@list)
{
if ($value eq $item)
{
print ("found $value\n");
$found = 1;
last;
}
}
.... several other foreach loops follow in which the only thing which
differs is the name of the array which is "@list" above, and the test for
the flag.
return
}
of a foreach loop with return. I have a function which searches for a
value in one of several arrays. If the value is found in any of them,
then the whole function may as well terminate. At the moment I am setting
a flag when the value is found, which stops execution of the later loops.
Would it be safe / stylistically sound to do away with the flag by using
"return" rather than "last". Example code follows.
TIA
sub example{
my $found;
$found = 0;
foreach $item (@list)
{
if ($value eq $item)
{
print ("found $value\n");
$found = 1;
last;
}
}
.... several other foreach loops follow in which the only thing which
differs is the name of the array which is "@list" above, and the test for
the flag.
return
}