Methods by type

Array mutation — push / pop / splice

Full

Grow, shrink, and edit an array in place at either end or in the middle.

An @ array is mutable. push/pop work at the end, shift/unshift at the front, and splice edits any range in the middle.

Both ends #

push and unshift add (to the end and front); the array grows in place.

my @a = 1, 2, 3;
@a.push(4);
@a.unshift(0);
say @a;
Output
[0 1 2 3 4]

pop and shift remove and return the removed element.

my @a = 1, 2, 3;
say @a.pop;
say @a.shift;
say @a;
Output
3
1
[2]

splice — edit the middle #

splice(start, count, replacement…) removes count elements at start and inserts the replacements.

my @a = <a b c d e>;
@a.splice(1, 2, "X");
say @a;
Output
[a X d e]

Two elements (b, c) are removed and one (X) inserted.

Notes #