> From: "Brent Dax" <[EMAIL PROTECTED]> > Date: Thu, 5 Dec 2002 00:28:52 -0800 > > Michael G Schwern: > # You can do it with a map without much trouble: > # > # my @indexes = map { /condition/ ? $i++ : () } @stuff; > > Unless I'm mistaken, that won't work, since $i only gets incremented on > matches. I think this: > > my @indexes = map { $i++; /condition/ ? $i : () } @stuff; > > Will work fine, though.
> Or, in the spirit of use-a-foreach-like-a-for (and my favorite WTDI): > > my @indexes = grep { $stuff[$_] =~ /condition/ } 0..$#stuff; Fantastic! One that doesn't use a variable temporary variable. We all write too quickly to catch errors like the one above. Except yours seems to be clean. Anyway---back to relevant topics. > As you might guess, I'm a (not very vocal) proponent of adding a way to > get at a foreach's (or map's or grep's) current index. (Hmm, can this > be done with XS? Must research...) In Perl 5 that would be nice. In Perl 6 though, it is not necessary: for zip(@array, 0..Inf) -> $v, $c { ... } That parallel iteration is just getting more and more useful (although this is a particularly ancient case). We've already been through A4, but the idea of C<for> has changed since then (I think). I don't like C<zip> as a name for such a thing. for interleave(@array, 0..Inf) {...} Too long. for slice (@array, 0..Inf) {...} for collate (@array, 0..Inf) {...} for parallel(@array, 0..Inf) {...} for braid (@array, 0..Inf) {...} for thread (@array, 0..Inf) {...} for weave (@array, 0..Inf) {...} The sequential list generator would almost certainly be C<each>, if we need one at all. for @array {...} for each @array: {...} for each(@array, @barry) {...} for @array, @barry {...} # Is this legal? That last one might iterate through the arrays themselves, not their elements, which would be useful on the blue moon nights. ?? Or is it still: for @array ; 0..Inf -> $v ; $c { ... } I hope not. Not to delay the next Apocalypse, or anything <:( Luke