Perl 6 Cool Perl 6 #18

Iterating hashes

Hashes in flattening context are a list of pairs:

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

for %scores -> $pair { say "{$pair.key} scored {$pair.value}"; }

PerlJam scored 78
pmichaud scored 52
mäsak scored 95

Shorter versions of the above:

for %scores { say "{.key} scored {.value}"; }

for %scores { say .fmt("%s scored %s"); }

say %scores.fmt("%s scored %s");
Copyright © 2010
http://www.pmichaud.com/2010/pres/