Documentation for routine append

Documentation for routine append, assembled from the following types:

class Any

From Any

(Any) method append

Defined As:

    method append(--> TODO)


class Array

From Array

(Array) method append

Defined as

sub append(\arrayelems)
multi method append(Array:D: \values)
multi method append(Array:D: **@values 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 with an array or list argument, append appends several elements (one for each array or list element), whereas push appends just one element.


my @a = <a b c>;
my @b = <d e f>;
@a.append: @b;
say @a.elems;               # OUTPUT: «6␤» 
say @a;                     # OUTPUT: «[a b c d e f]␤» 

class Hash

From Hash

(Hash) method append

Defined as:

method append(+@values)

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 Pairs or colon pairs will be treated as named arguments to .append.

my %h = => 1;
%h.append({=> 4});
say %h;
# OUTPUT: «{a => 1, b => 2, c => 3, d => 4}␤» 
# 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 %hb = :a[42, ]; %hb.append: "a" => <a b c a>;
say %hb# OUTPUT: «{a => [42 a b c a]}␤» 
my %ha = :a[42, ]; %ha.push: "a" => <a b c a>;
say %ha# OUTPUT: «{a => [42 (a b c a)]}␤» 

class Nil

From Nil

(Nil) method append

method append(*@)

Warns the user that they tried to append onto a Nil.