method append
1 | class Hash |
1.1 | (Hash) method append |
2 | class Any |
2.1 | (Any) method append |
3 | role Buf |
3.1 | (Buf) method append |
4 | class Array |
4.1 | (Array) method append |
5 | class Nil |
5.1 | (Nil) method append |
Documentation for method append
assembled from the following types:
class Hash
From Hash
(Hash) method append
Defined as:
method append(+)
Append the provided Pairs or even sized list to the Hash. If a key already exists, turn the existing value into an Array and push new value onto that Array
. Please note that you can't mix even sized lists and lists of Pairs. Also, bare Pair
s or colon pairs will be treated as named arguments to .append
.
my = a => 1;.append('b', 2, 'c', 3);.append( %(d => 4) );say ;# OUTPUT: «{a => 1, b => 2, c => 3, d => 4}».append('a', 2);# OUTPUT: «{{a => [1 2], b => 2, c => 3, d => 4}»
Note: Compared to push
, append
will slip
in the given value, whereas push
will add it as is:
my = :a[42, ]; .append: "a" => <a b c a>;say ; # OUTPUT: «{a => [42 a b c a]}»my = :a[42, ]; .push: "a" => <a b c a>;say ; # OUTPUT: «{a => [42 (a b c a)]}»
class Any
From Any
(Any) method append
Defined as:
multi method append(Any \SELF: |values --> Array)
In the case the instance is not a positional-thing, it instantiates it as a new Array, otherwise clone the current instance. After that, it appends the values passed as arguments to the array obtained calling Array.append
on it.
my ;say .append; # OUTPUT: «[]»my ;say .append((1,2,3)); # OUTPUT: «[1 2 3]»
role Buf
From Buf
(Buf) method append
method append( )
Appends at the end of the buffer
.append( [5..10] );say .perl; # OUTPUT: «Buf.new(1,1,2,3,5,8,13,21,34,55,89)»
class Array
From Array
(Array) method append
Defined as
sub append(\array, |elems)multi method append(Array: \values)multi method append(Array: ** is raw)
Appends the argument list to the array passed as the first argument. This modifies the array in-place. Returns the modified array. Throws for lazy arrays.
The difference from method push
is that if you append a single array or list argument, append
will flatten that array / list, whereas push
appends the list / array as just a single element.
Example:
my = <a b c>;my = <d e f>;.append: ;say .elems; # OUTPUT: «6»say ; # OUTPUT: «[a b c d e f]»
class Nil
From Nil
(Nil) method append
method append(*@)
Warns the user that they tried to append onto a Nil
.