Course of Raku / Essentials / Associative data types


A hash is another example of associative data types in Raku. It is an aggregate data type that maps the names of its items to their values. Let us introduce the new sigil that hash variables use: %.

my %capitals;

You can now use the hash and assign some initial values to it.

%capitals = France => 'Paris', Italy => 'Rome';

As with other data types, both actions can be done together:

my %capitals = France => 'Paris', Italy => 'Rome';

Accessing the elements

Subscripting the elements is similar to what we’ve seen for pairs. Use a pair of angle brackets or curly braces with a string:

say %capitals<France>;
say %capitals{'Italy'};

Hashes are mutable, so you can both change the existing values as well as add the new ones:

%capitals<Germany> = 'Berlin';

Keys and values

The two built-in methods, keys and values, return the lists of the keys and the values of a hash:

say %capitals.keys;
say %capitals.values;

Here is an example of the output of this program:

(Germany Italy France)
(Berlin Rome Paris)

Notice that hash elements are not ordered, but the order of the keys and values, returned by the above methods, is identical.


Complete the quizzes that cover the contents of this topic.

Course navigation

Associative data types / Pairs   |   Associative data types / Nested hashes

💪 Or jump directly to the exercises to this section.