Raku kurss / Pamati / Asociatīvie datu tipi

Hashes

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.

Prakse

Pabeidziet viktorīnas, kas aptver šīs tēmas saturu.

Kursa navigācija

Asociatīvie datu tipi / Pāri   |   Asociatīvie datu tipi / Ligzdoti hashes


💪 Or jump directly to the exercises to this section.

Šī lapa ir automātiski tulkota ar ChatGPT 4o. Oriģinālais teksts angļu valodā.

Šīs lapas tulkojumi: EnglishDeutschEspañolItalianoLatviešuNederlandsБългарскиРусскийУкраїнська