T
Tim Shoppa
OK, I'm a former (and current!) Fortran programmer. But often I end up
with Perl data structures where I not only have to step through each element of
an array, but also must know the index into the array.
I end up writing code that looks like
for my $i (0..$#a) {
my $e = $a[$i];
# Do stuff with $i and $e here
}
That's a bit ugly, I have a numeric loop, an extra variable, that funny
(0..$#a) construct, etc. And if I have to "last" to bail out of the loop
I've lost my index and the array element that I bailed out at.
I'd rather write something like
for (@a) {
# Do stuff with $_ and some magical variable telling me the index
}
in the same way that I can
while (<>) {
# Do stuff with $_ and the line number $. here
}
Is there some magical variable like $. for array loops? Any better
idioms than what I'm currently doing?
Tim.
with Perl data structures where I not only have to step through each element of
an array, but also must know the index into the array.
I end up writing code that looks like
for my $i (0..$#a) {
my $e = $a[$i];
# Do stuff with $i and $e here
}
That's a bit ugly, I have a numeric loop, an extra variable, that funny
(0..$#a) construct, etc. And if I have to "last" to bail out of the loop
I've lost my index and the array element that I bailed out at.
I'd rather write something like
for (@a) {
# Do stuff with $_ and some magical variable telling me the index
}
in the same way that I can
while (<>) {
# Do stuff with $_ and the line number $. here
}
Is there some magical variable like $. for array loops? Any better
idioms than what I'm currently doing?
Tim.