📘 Hash in Perl 6

Hashes provide a few methods with clear semantics, for instance:

my %hash = Language => 'Perl', Version => 6;

say %hash.elems;  # number of pairs in the hash
say %hash.keys;   # the list of the keys
say %hash.values; # the list of the values

Here’s the output:

 2
(Version Language)
(6 Perl)

It is possible to iterate not only over the hash keys or values but also over whole pairs:

for %hash.pairs {
    say $_.key;
    say $_.value;
}

The .kv method returns a list containing the alternating keys and values of the hash:

say %hash.kv # (Version 6 Language Perl)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s