Skip to content

Commit

Permalink
Add an example for Hash.push
Browse files Browse the repository at this point in the history
  • Loading branch information
ab5tract committed Jun 9, 2015
1 parent 16c4d0c commit 2b7d3ce
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/Type/Hash.pod
Expand Up @@ -44,14 +44,14 @@ If an L<Enum> is encountered where a value is expected, it is used as a
hash value:
my %h = 'a', 'b' => 'c';
say %h<a>.WHAT; # Pair();
say %h<a>.key; # b
say %h<a>.WHAT; # Pair();
say %h<a>.key; # b
If the same key appears more than once, the value associated with its last
occurrence is stored in the hash:
my %h = a => 1, a => 2;
say %h<a>; # 2
say %h<a>; # 2
=head1 Looping over hash keys and values
Expand All @@ -60,7 +60,7 @@ keys and values, for instance,
my %vowels = 'a' => 1, 'e' => 2, 'i' => 3, 'o' => 4, 'u' => 5;
for %vowels.kv -> $vowel, $index {
"$vowel: $index".say;
"$vowel: $index".say;
}
gives output similar to this:
Expand All @@ -82,7 +82,7 @@ list of vowels in alphabetical order then one would write
my %vowels = 'a' => 1, 'e' => 2, 'i' => 3, 'o' => 4, 'u' => 5;
for %vowels.sort(*.key)>>.kv -> ($vowel, $index) {
"$vowel: $index".say;
"$vowel: $index".say;
}
which prints
Expand Down Expand Up @@ -111,13 +111,20 @@ is a C<List>.
Adds the C<@new> elements to the hash with the same semantics as hash
assignment, but with three exceptions:
=item the hash isn't emptied first, i.e. old pairs are not deleted.
=item The hash isn't emptied first, i.e. old pairs are not deleted.
=item if a key already exists in the hash, and the corresponding value is an
=item If a key already exists in the hash, and the corresponding value is an
L<Array>, the new value is pushed onto the array (instead of replacing it).
=item if a key already exists in the hash, and the corresponding value is not
=item If a key already exists in the hash, and the corresponding value is not
an L<Array>, old and new value are both placed into an array in the place
of the old value.
Example:
my %h = a => 1;
%h.push: a => 1; # a => [1,1]
%h.push: a => [ 1 xx 3 ]; # a => [1,1,1,1,1]
%h.push: b => 3; # a => [1,1,1,1,1], b => 3
=end pod

0 comments on commit 2b7d3ce

Please sign in to comment.