The problem I have with each is the "while" loop feels counterintuitive to what
you're trying to do. IIRC, in Perl 6 there will be a .kv() method on both
hashes and arrays, returning a (lazy) list of keys and values interspersed (or
maybe key value pairs...hmmm).
for %hash.kv -> $k, $v {
# ...
}
for @array.kv -> $idx, $elt {
# ...
}
On Wed, 2 Jul 2003, Xavier Noria wrote:
> Thinking about the convenience of the current behaviour of the range
> operator as a way to easily iterate through an array when one needs
> both the index and the element, as some have stated, I wonder whether
> the lack of a specific idiom for this as Ruby has is on purpose.
>
> For instance, we could have:
>
> foreach_with_idx my ($idx, $elt) (@array) {
> # ...
> }
>
> I don't like that very much indeed. If it was kind of a foreach it
> should IMHO resemble it as much as possible, but I don't imagine a
> suitable special variable for the index in
>
> foreach_with_idx (@array) {
> # where would the index be stored?
> }
>
> and the use as a modifier is not clear as well.
>
> The parallelism with each() for hashes is however so clear to me that if
> I could I would probably suggest
>
> while (my ($idx, $elt) = each @array) {
> # ...
> }
>
> Does that each() have been discussed and discarded previously? Do you
> see any drawbacks to it?
>
> fxn
>
--
Adam Lopresto
http://cec.wustl.edu/~adam/
I don't want to achieve immortality through my work. I want to achieve
immortality through not dying.
-- Woody Allen
|