Lesson 9 of 15

Hashes

Hashes

Hashes (associative arrays) map keys to values, prefixed with %:

my %ages = (
    alice => 30,
    bob   => 25,
    carol => 35,
);

The => (fat comma) automatically quotes the key on its left.

Accessing Values

Use $hash{key}:

say $ages{alice};   # 30
say $ages{bob};     # 25

Adding / Modifying Entries

$ages{dave} = 28;        # add new key
$ages{alice} = 31;       # update existing

Deleting Entries

delete $ages{bob};

Checking Existence

if (exists $ages{carol}) {
    say "found carol";
}

Your Task

Create a hash with three people and their ages, then print each person's name and age.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.