routine push

Documentation for routine push assembled from the following types:

class Nil

From Nil

(Nil) method push

method push(*@)

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

role Buf

From Buf

(Buf) method push

method push$elems )

Adds elements at the end of the buffer

my @φ =  1,1* + * … ∞;
my $ =φ[^5] );
$.push@φ[5] );
say $.perl# OUTPUT: «,1,2,3,5,8)»

class Array

From Array

(Array) routine push

Defined as:

multi sub    push(Array:D**@values --> Array:D)
multi method push(Array:D: **@values --> Array:D)

Adds the @values to the end of the array, and returns the modified array. Throws for lazy arrays.


my @foo = <a b c>;
@foo.push: 'd';
say @foo;                   # OUTPUT: «[a b c d]␤»

Note that push does not attempt to flatten its argument list. If you pass an array or list as the thing to push, it becomes one additional element:

my @a = <a b c>;
my @b = <d e f>;
@a.push: @b;
say @a.elems;               # OUTPUT: «4␤» 
say @a[3].join;             # OUTPUT: «def␤»

Only if you supply multiple values as separate arguments to push are multiple values added to the array:

my @a = '1';
my @b = <a b>;
my @c = <E F>;
@a.push: @b@c;
say @a.elems;                # OUTPUT: «3␤»

See method append for when you want to append multiple values that are stored in a single array.

class Any

From Any

(Any) method push

Defined as:

method push(|values --> Positional:D)

The method push is defined for undefined invocants and allows for autovivifying undefined to an empty Array, unless the undefined value implements Positional already. The argument provided will then be pushed into the newly created Array.

my %h;
say %h<a>;     # OUTPUT: «(Any)␤»      <-- Undefined 
%h<a>.push(1); # .push on Any 
say %h;        # OUTPUT: «{a => [1]}␤» <-- Note the Array

class Hash

From Hash

(Hash) method push

Defined as:

multi method push(Hash:D: *@new)

Adds the @new elements to the hash with the same semantics as hash assignment, but with three exceptions:


my %h  = => 1;
%h.push: (=> 1);              # a => [1,1] 
%h.push: (=> 1xx 3 ;        # a => [1,1,1,1,1] 
%h.push: (=> 3);              # a => [1,1,1,1,1], b => 3 
%h.push('c' => 4);              # a => [1,1,1,1,1], b => 3, c => 4 
push %h'd' => 5;              # a => [1,1,1,1,1], b => 3, c => 4, d => 5

Please note that Pairs or colon pairs as arguments to push will be treated as extra named arguments and as such wont end up the Hash. The same applies to the sub push.

my %h .= push(=> 6);
push %h=> 7;
say %h.perl;
# OUTPUT: «{}␤»

Also note that push can be used as a replacement for assignment during hash initialization very useful ways. Take for instance the case of an inverted index:

my %wc = 'hash' => 323'pair' => 322'pipe' => 323;
(my %inv).push: %wc.invert;
say %inv;                     # OUTPUT: «{322 => pair, 323 => [pipe hash]}␤»

Note that such an initialization could also be written as

my %wc = 'hash' => 323'pair' => 322'pipe' => 323;
my %inv .= push: %wc.invert;

Note: Compared to append, push will add the given value as is, whereas append will slip it in:

my %ha = :a[42, ]; %ha.push: "a" => <a b c a>;
say %ha# OUTPUT: «{a => [42 (a b c a)]}␤» 
my %hb = :a[42, ]; %hb.append: "a" => <a b c a>;
say %hb# OUTPUT: «{a => [42 a b c a]}␤»