Skip to content

Commit

Permalink
Hash: clarify the looping over sorted keys example
Browse files Browse the repository at this point in the history
  • Loading branch information
rudis committed Feb 21, 2016
1 parent dafabe7 commit d70a800
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions doc/Type/Hash.pod
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ which prints
and is in alphabetical order as desired. To achieve this result, we sorted
the hash of vowels by key (C<%vowels.sort(*.key)>) which we then ask for its
keys and values by applying the C<.kv> method to each element via the unary
C< >> > hyperoperator. The output of this operation is a L<Seq>, the
items of which need to be passed into the C<for> loop's block as arguments,
which is why you will find the key/value pairs wrapped in parentheses. The signature
of the point block could also be a single argument.
C< >> > hyperoperator resulting in a L<List> of key/value lists. To extract
the key/value the variables thus need to be wrapped in parentheses.
for %vowels.kv -> $vowel {
say $vowel;
}
An alternative solution is to flatten the resulting list. Then the key/value
pairs can be accessed in the same way as with plain C<.kv>:
This would print the list one line at a time.
my %vowels = 'a' => 1, 'e' => 2, 'i' => 3, 'o' => 4, 'u' => 5;
for %vowels.sort(*.key)>>.kv.flat -> $vowel, $index {
"$vowel: $index".say;
}
=head2 Object hashes and type constraints
Expand Down

0 comments on commit d70a800

Please sign in to comment.