in reply to Can I do this?
print "I: $i\n";
Generally, loops like this are discouraged in Perl because there are better ways to do things. I might write your code like this:
By the end of my snippet, $pos will either contain the appropriate index, or will still be 0. It's shorter and needs no labels. Best of all, it doesn't need $i, so the question is moot.my $pos = 0; for ($j .. $#array) { if ($array[$_] eq 'blah') { $pos = $_; last; } }
I hope this helps.
Update: HyperZonk suggests that the original poster may want to search the first half of the array if $j through the end didn't have the element. In that case, maybe the C-style loop is better:
Untested, but it's one way to do it.my ($i, $end) = ($j, $#array); my $pos = 0; for (; $i <= $end; $i++) { last if ($array[$i] eq 'blah'); if ($i == $#array) { $i = 0; $end = $j - 1; } }
|
|---|