Operators

Slices

Full

Index an array or hash with several keys at once to get several values.

Subscripting with a list of keys returns a list of values — a slice. It works on arrays (by position) and hashes (by key), and the index list can itself be a range or use the Whatever star.

Array slices #

my @a = 10, 20, 30, 40;
say @a[1, 3];
say @a[1..2];
say @a[*-1, 0];
Output
(20 40)
(20 30)
(40 10)

Any list of indices works — explicit (1, 3), a range (1..2), or computed (*-1 for the last).

Hash slices #

Subscript a hash with several keys to pull several values, in the order asked.

my %h = a => 1, b => 2, c => 3;
say %h<a c>;
say %h{"a", "b"};
Output
(1 3)
(1 2)

<a c> is the quoted-word form (constant keys); {"a", "b"} takes an expression list.

Notes #