Methods by type
Hash methods
FullInspect 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
FalseSorting 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 #
.keys,.values, and.pairsreturn their results in a matching but unspecified order —%h.keys Z %h.valuesstays aligned, but sort for reproducible output.:deleteis the sibling adverb of:exists:%h<a>:deleteremoves the key and returns its value..kvflattens to an alternatingkey, value, key, value…list — convenient forfor %h.kv -> $k, $v { … }.