Hash
Talk0
77pages on
this wiki
this wiki
A data structure that stores pairs.
- Mutable: True
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.