Re: RFC 120 (v2) Implicit counter in for statements, possibly $#.
[email protected] (Ariel Scolnicov) 18 Aug 2000 23:21:28 +0300
| Newsgroups | perl.perl6.language.flow |
|---|---|
| Organization | Compugen, Ltd. |
| Message-ID | <[email protected]> |
"Christopher J. Madsen" <[email protected]> writes: > Jonathan Scott Duff writes: > > Excellent summary of why an explicit index is a Good Thing as compared > > to the programmer doing it himself. I think the syntax would need to > > be different though, how do you use implicit $_ and an index? (Don't > > Do That is not an answer because people will want it) Here are some > > ideas: > > > > # Gosh, let's overuse : like python! ;-) > > for $item:$index (@array) { ... } > > for :$index (@array) { ... } # $_ and counter > > > > for (@array : index($index)) { ... } > > > > for (@array) $index { ... } > > I would say you can't use implicit $_ with an index; you use explicit > $_ instead. > > foreach $_ $index (@array) { ... } Others have commented on how easy is it to get this wrong. May I propose an alternate syntax? Instead of trying to hack C<for> loops to do what you want, extend C<each> to iterate over arrays in-order. Then the examples become: while (($index,$item) = each @array) { ... } Actually, this suggests that C<values @array> should return C<(@array)> (a copy of the values), and C<keys @array> the list C<(0..$#array)>. But those aren't very useful. [...] -- Ariel Scolnicov |"GCAAGAATTGAACTGTAG" | [email protected] Compugen Ltd. |Tel: +972-2-6795059 (Jerusalem) \ We recycle all our Hz 72 Pinhas Rosen St. |Tel: +972-3-7658514 (Main office)`--------------------- Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555 http://3w.compugen.co.il/~ariels