Mostly Harmless Docs/Hashes

my %h = 'a' => 1, 'b' => 'two', 'c' => 3;
# Same. Fat-comma autoquotes what's on its left.
%h = a => 1, b => 'two', c => 3;

%h = :a(1), :b('two'), :c(3);  # same
%h = :a(1), :b<two>, :c(3);    # same (Pair ctor shorthand autoquotes)

# Same
my %h = {};
%h{'a'} = 1;
%h<b> = 'two';
%h<c> = 3;

# You can also use .push on a hash, which adds additional
# values on a key by creating a list and pushing to that:
my %h2 = {};
%h2.push('a', 11)   # ("a" => 11).hash
%h2.push('b', 1)    # ("a" => 11, "b" => 1).hash
%h2.push('a', 22)   # ("a" => [11, 22], "b" => 1).hash
%h2.push('a', 33)   # ("a" => [11, 22, 33], "b" => 1).hash

%h<b>           #=> two
%h{'b', 'a'}    #=> two 1
%h<b a>         #=> same, but more convenient syntax

%h.exists('a')  #=> True, if the hash has a key 'a' in it.

"hi %h<a> bye"  #=> hi 1 bye
"hi {%h} bye"   #=> hi a 1 b two c 3 bye

%h.delete('b')  # deletes the :b<two> pair from the hash

%h.perl         #=> ("a" => 1, "c" => 3).hash

%h.elems        #=> 2   (number of elements/Pairs)
%h.keys         #=> a c
%h.values       #=> 1 3

Iterating over a hash is covered in Control Structures.


Main | Next: Special Variables