Data structures · lesson 8 of 18
Hashes
Key–value maps with the % sigil — look up with <...>, add pairs, test existence, iterate.
A hash maps keys to values and wears the % sigil. Build one from pairs (key => value) and look values up with <...>:
my %age = alice => 30, bob => 25; say %age<alice>; say %age<bob>;
Output
30
25For a key held in a variable, use curly braces instead: %age{$name}.
my %age = alice => 30, bob => 25;
my $who = 'bob';
say %age{$who};
Output
25Adding, testing, counting #
Assign to a new key to add it. The :exists adverb asks whether a key is present:
my %age = alice => 30; %age<carol> = 28; say %age.elems; say %age<carol>:exists; say %age<dave>:exists;
Output
2
True
FalseIterating #
A hash has no fixed order, so sort before you print. Each element is a Pair with .key and .value:
my %population = tokyo => 37, delhi => 32, paris => 11;
for %population.sort -> $p {
say "{$p.key}: {$p.value} million";
}
Output
delhi: 32 million
paris: 11 million
tokyo: 37 millionTry it #
Count the letters of banana into a hash, then print the counts in sorted order. (Hint: 'banana'.comb gives the list of letters, and %count{$_}++ counts one.)
my %count;
for 'banana'.comb {
}
Show a solution
my %count;
for 'banana'.comb {
%count{$_}++;
}
for %count.sort -> $p {
say "{$p.key}: {$p.value}";
}
Output
a: 3
b: 1
n: 2