Wikia

Perl6 Wiki

Hash

Talk0
77pages on
this wiki

A data structure that stores pairs.

Building a hash Edit

The simplest way to create a hash is by passing it a list of pairs

my %pets = (
   "cats" => 3,
   "dogs" => 4,
   "rabbits" => 0
)

Or, you can create a hash without defining a list, but a block with pairs instead.

my %pets = {
   "cats" => 3,
   "dogs" => 4,
   "rabbits" => 0
}
You can also make a hash grow by defining a hash pair
%pets{"fishes"} = 4;

Traversing a hash Edit

As each hash "node" contains 2 values, you can use some hash methods to obtain what you need

my @h_keys = %pets.keys;
my @h_values = %pets.values;

for (%pets.kv) -> $kind, $ammount {
   say "there are $ammount $kind in my house ";
}


Hash instances do not have a specific order.

See also Edit

Advertisement | Your ad here

Photos

Add a Photo
10photos on this wiki
See all photos >

Recent Wiki Activity

See more >

Around Wikia's network

Random Wiki