Suppose I have an array @list, and I want to know where "foo" is in the list. Is there a short command to return that "foo" is the nth element of the array?
if you expect "foo" to be in the list once you may want to also use "last" to end the loop early:
Code:
my $found = 0;
for my $i (0 .. $#array) {
if ($array[$i] eq 'foo') {
print "'foo' found at position: $i\n";
$found++;
last;
}
}
print "'foo' not found\n" unless ($found);
@test=qw(a b c d foo e f g h);
for (0 .. $#test) {print && last if($test[$_] eq "foo")};
Reasonably concise.
My next thought was that if you're gonna do lots of this on that data then maybe you'd be better creating a hash where the keys are your array values and the hash values are the indexes into the array.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.