I don't see why this should be an implicit counter. This (might)
cause extra work for every foreach loop in every program (depending on
how foreach is implemented).
Why not use an explicit counter instead? Something like
foreach $item $index (@array) {
print $item, " is at index ",
David L. Nicol writes:
Why not use an explicit perl5 counter?
my $index;
foreach $item (@array){ $index++;
print $item, " is at index ", $index, "\n";
}
Well, one reason is that your example doesn't work (it starts the
index at 1 instead of 0). You'd need to do
Tom Christiansen writes:
One could argue that do{} should take return so it might have a value,
but this will definitely annoy the C programmers.
So what.
So what is that it *already* annoys us, which is *why* we would like to
last out of a do. Perhaps you should be able to last
Peter Scott writes:
I dunno, maybe a last in a do block whose value is used by
something should be a syntax error. We're talking about code like
$x += do { $y = get_num; last if $y == 99; $y } while defined $y;
right? *Shudder*
Yes, but we're also talking about code like