Hashes
Declaring a hash
A hash is an unordered collection of key-value pairs, marked by the percent sign. Keys are strings, values are scalars, and the whole list sits in parentheses as key-value, key-value.
my %user = ( "name" => "Ada", "age" => 36, "city" => "London", ); print "$user{name}\n"; # Ada
perl demo.pl
Ada
Order is not preserved and not promised. If you print the hash directly you get the pairs in whatever order the internal table happens to have, so sort the keys whenever output must be stable.
Read and write
One key is one scalar, so the sigil is a dollar sign. Writing to a key adds it or replaces the old value, and there is no separate update call.
my %user = ("name" => "Ada", "age" => 36, "city" => "London"); $user{age} = 37; $user{role} = "admin"; print "$user{role}\n"; # admin
perl demo.pl
admin
Reading a key that was never written returns undef, and under use warnings you get a warning if you then use it as a string. Check with exists, which lesson 3.4 covers, before you trust a key.
The fat comma
=> is called the fat comma. It does exactly what a comma does, but it also quotes a bare word on its left, which is why hash literals read like a table instead of a string list.
my %limits = ( cpu => 80, mem => 90, disk => 95, ); print $limits{disk}, "\n"; # 95
perl demo.pl
95
Keys with spaces or punctuation still need quotes, because the bare word rule only covers simple identifiers. Keep keys short and lower case and the literals stay readable.
Autovivification
Reading a missing key in a nested structure can create it, a behavior called autovivification. Counting with ++ relies on it: the key springs into existence with the value 1.
my %hits = (); $hits{'/index.html'}++; $hits{'/index.html'}++; print $hits{'/index.html'}, "\n"; # 2
perl demo.pl
2
The same feature bites in conditions: if ($user{prefs}{theme}) happily creates an empty prefs hash just to look inside it. Use exists on each level when a missing branch must stay missing.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which sigil marks a hash?
Answer
%The percent sign names the whole key-value collection, as
@names a list and$names one scalar. -
Why is one hash element written
$user{name}and not%user{name}?Answer
Because one key holds one scalar value, so the sigil is the scalar one
The sigil follows the value you take from the name: the whole hash is
%user, one value is$user{name}. -
What order do the pairs of a hash come back in when you print the hash directly?
Answer
Whatever order the internal table happens to have, so sort the keys when output must be stable
A hash is unordered by design;
sort keys %his how a program gets a repeatable order. -
What does the fat comma
=>do that a plain comma does not?Answer
It quotes a bare word on its left, so hash literals read like a table
=>is a comma with autoquoting of the left side, which is whycpu => 80needs no quotes around cpu. -
What does reading a key that was never written give you?
Answer
undef, with a warning if you then use it as a stringA missing key is
undef;existstells you whether the key is there before you rely on it. -
What is autovivification?
Answer
Reading a missing key of a nested structure creates the intermediate containers
Looking inside
$user{prefs}{theme}creates$user{prefs}as an empty hash, which is handy for counting and surprising in conditions. -
What does this program print?
my %user = (name => "Ada", age => 36); print $user{name}, "\n";Answer
Ada
The key name holds the string Ada, and one key is one scalar, so the dollar sigil reads it.
-
What does this program print?
my %h = (a => 1); $h{a} = 5; print $h{a}, "\n";Answer
5
Writing to an existing key replaces its value; nothing is added or counted.
-
What does this program print?
my %hits = (); $hits{page}++; $hits{page}++; print $hits{page}, "\n";Answer
2
The first
++brings the key into existence with 1, and the second makes it 2. -
What does this program print?
my %limits = (cpu => 80, mem => 90); print $limits{mem}, "\n";Answer
90
The fat comma quotes the keys, so
memholds 90 and the lookup finds it.
Article author: Arthur Isaev