Course of Raku / Essentials / Associative data types

Nested hashes

Creating nested hashes is similar to creating nested arrays. Use curly braces to embrace the inner hashes.

my %people =
    John => {
        age => 20,
        city => 'Madrid'
    },
    Alla => {
        age => 21,
        city => 'Tokyo'
    };

Note that a trailing comma is a valid syntax element:

my %people =
    John => {
        age => 20,
        city => 'Madrid',
    },
    Alla => {
        age => 21,
        city => 'Tokyo',
    };

Having a comma helps when you edit the structure by copying and pasting the lines.

To get the elements from the inner hashes, use two keys one after another.

say %people<John><city>; # Madrid
say %people<Alla><city>; # Tokyo

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Associative data types / Hashes   |   Associative data types / Interpolating hashes


💪 Or jump directly to the exercises to this section.