On Thu, Dec 25, 2008 at 12:39:24PM -0500, Mark J. Reed wrote:
> On Thu, Dec 25, 2008 at 12:00 PM, Patrick R. Michaud <pmich...@pobox.com> 
> wrote:
> > On Thu, Dec 25, 2008 at 12:53:06AM -0500, Mark J. Reed wrote:
> >> I also tried this, but it caused Rakudo to throw a StopIteration and
> >> then segfault:
> >>
> >> for [...@gifts[0..$day-1]].pairs.reverse -> $n, $g
> >
> > The StopIteration occurs when there aren't enough elements in the
> > list to supply to the parameters to the body.  In the example above,
> > it would occur whenever there are an odd number of pairs.
> OK, so that loops through the list in groups of two.  So how do I Ioop
> through a list of Pairs assigning the key to one var and the value to
> another?  It's not a Hash, and calling .kv on it yields 0=>first pair,
> 1=>second pair, etc... I don't want to Hashify it because I want to
> preserve the order...

Extracting the keys+values from a list of Pairs was a question that
occurred to me as well when implementing it... I don't know the answer
to that.

However, for this specific problem, perhaps the zip operator is a
better choice anyway:

    for (^$day Z @gifts).reverse -> $g, $n { ... }

I'm not entirely sure of the order of the $g, $n arguments here,
or exactly how .reverse chooses to change the order of the items
produced by infix:<Z> (does it flatten or no?).  But some permutation
of this should work.


Reply via email to