Methods by type

Hash methods

Full

Inspect and order associative data — keys, values, pairs, :exists, sorting.

A Hash maps keys to values. Its methods let you pull the keys, values, or key/value pairs, test membership, and order the entries (hash iteration itself is unordered, so sort when you need determinism).

keys and values #

my %h = a => 1, b => 2, c => 3;
say %h.keys.sort;
say %h.values.sort;
Output
(a b c)
(1 2 3)

pairs #

pairs yields key => value Pair objects — handy for iterating entries together.

my %h = a => 1, b => 2, c => 3;
say %h.pairs.sort;
Output
(a => 1 b => 2 c => 3)

Testing membership — :exists #

The :exists adverb on a subscript asks whether a key is present, without autovivifying it.

my %h = a => 1, b => 2;
say %h<a>:exists;
say %h<z>:exists;
Output
True
False

Sorting by value #

sort with a key extractor orders the pairs; here by value, descending, taking the keys.

my %h = a => 1, b => 2;
say %h.sort(*.value).reverse.map(*.key);
Output
(b a)

Notes #