Perl 6 Cool Perl 6 #17

Iterating n values at a time

We can use this feature to automatically extract keys and values from a hash:

my %scores = < pmichaud 52   mäsak 95   PerlJam 78 >;

for %scores.kv -> $name, $score {
    say "$name has $score points";
}

The .kv method returns elements of a hash as a list with interleaved keys and values.

It also works with lists -- the keys are the indexes in the list:

my @list = <apple cherry banana lemon>;
for @list.kv -> $i, $type { say "$i: $type"; }

See also .keys, .values, and .pairs methods

Copyright © 2010
http://www.pmichaud.com/2010/pres/