Skip to content

Commit 2b7d3ce

Browse files
committed
Add an example for Hash.push
1 parent 16c4d0c commit 2b7d3ce

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lib/Type/Hash.pod

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ If an L<Enum> is encountered where a value is expected, it is used as a
4444
hash value:
4545
4646
my %h = 'a', 'b' => 'c';
47-
say %h<a>.WHAT; # Pair();
48-
say %h<a>.key; # b
47+
say %h<a>.WHAT; # Pair();
48+
say %h<a>.key; # b
4949
5050
If the same key appears more than once, the value associated with its last
5151
occurrence is stored in the hash:
5252
5353
my %h = a => 1, a => 2;
54-
say %h<a>; # 2
54+
say %h<a>; # 2
5555
5656
=head1 Looping over hash keys and values
5757
@@ -60,7 +60,7 @@ keys and values, for instance,
6060
6161
my %vowels = 'a' => 1, 'e' => 2, 'i' => 3, 'o' => 4, 'u' => 5;
6262
for %vowels.kv -> $vowel, $index {
63-
"$vowel: $index".say;
63+
"$vowel: $index".say;
6464
}
6565
6666
gives output similar to this:
@@ -82,7 +82,7 @@ list of vowels in alphabetical order then one would write
8282
8383
my %vowels = 'a' => 1, 'e' => 2, 'i' => 3, 'o' => 4, 'u' => 5;
8484
for %vowels.sort(*.key)>>.kv -> ($vowel, $index) {
85-
"$vowel: $index".say;
85+
"$vowel: $index".say;
8686
}
8787
8888
which prints
@@ -111,13 +111,20 @@ is a C<List>.
111111
Adds the C<@new> elements to the hash with the same semantics as hash
112112
assignment, but with three exceptions:
113113
114-
=item the hash isn't emptied first, i.e. old pairs are not deleted.
114+
=item The hash isn't emptied first, i.e. old pairs are not deleted.
115115
116-
=item if a key already exists in the hash, and the corresponding value is an
116+
=item If a key already exists in the hash, and the corresponding value is an
117117
L<Array>, the new value is pushed onto the array (instead of replacing it).
118118
119-
=item if a key already exists in the hash, and the corresponding value is not
119+
=item If a key already exists in the hash, and the corresponding value is not
120120
an L<Array>, old and new value are both placed into an array in the place
121121
of the old value.
122122
123+
Example:
124+
125+
my %h = a => 1;
126+
%h.push: a => 1; # a => [1,1]
127+
%h.push: a => [ 1 xx 3 ]; # a => [1,1,1,1,1]
128+
%h.push: b => 3; # a => [1,1,1,1,1], b => 3
129+
123130
=end pod

0 commit comments

Comments
 (0)