Hashes
A hash is a set of key/value pairs. Hashes are
preceded by a percent sign (%
).
To refer to a single element of a hash, you use the hash variable
name followed by the “key” associated with the value in braces.
For example, the hash:
%fruit = ('apples', 3, 'oranges', 6);
has two values (in key/value pairs). If you want to get the
value associated with the key apples
, you use $fruit{'apples'}
.
It is often more readable to use the =>
operator in defining key/value
pairs. The =>
operator is
similar to a comma, but it’s more visually distinctive and quotes
any bare identifiers to the left of it:
%fruit = ( apples => 3, oranges => 6 );
Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.