Re: RFC 120 (v1) Implicit counter in C<for> statements, possibly C<$#>.
[email protected] (Tim Jenness) Wed, 16 Aug 2000 11:42:20 -1000 (HST)
| Newsgroups | perl.perl6.language.flow |
|---|---|
| Message-ID | <[email protected]> |
On 16 Aug 2000, Perl6 RFC Librarian wrote: > This and other RFCs are available on the web at > http://dev.perl.org/rfc/ > > =head1 TITLE > > Implicit counter in C<for> statements, possibly C<$#>. > > =head1 VERSION > > Maintainer: John McNamara <[email protected]> > Date: 16 Aug 2000 > Version: 1 > Mailing List: [email protected] > Number: 120 > > However, the latter format is often more convenient if you need to keep track of the array index as well as iterating over the array: > > for ($i = 0; $i <= $#array; $i++) { > print $array[$i], " is at index ", $i, "\n"; > } > > is more succinct than: > > $i = 0; > foreach $item (@array) { > print $item, " is at index ", $i, "\n"; > $i++; > } > What about: for (0..$#array) { print $array[$i], " is at index ", $i, "\n"; } I use that whenever I need to loop over indices of two arrays at once. -- Tim Jenness JCMT software engineer/Support scientist http://www.jach.hawaii.edu/~timj