... it wastes time, and extra space, to go back and modify the foreach loop to the equivalent for loop (ignoring for a moment the synonymity of foreach and for):
for (my $i = 0; $i < @array; $i++) { my $item = $array[$i]; do_something_with($item, $i); }Even a shorter idiom takes an extra line, and isn't quite as "clean" (imo), as the $i has to be defined outside of the loop:
my $i; foreach (@array) { do_something_with($item, $i++); }
I think the following is just as simple as using state or any other variation, and doesn't require any more lines of code than your original:
...unless I'm missing something.for my $i (0..$#array) { do_something_with($array[$i], $i); }
In reply to Re: Interesting Use for "state" Variables
by lostjimmy
in thread Interesting Use for "state" Variables
by liverpole
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |