Ad space (banner)
Lesson 9 / 20

Working with hashes (%)

This lesson covers the hash, which manages data as key-value pairs.

A hash is declared as %name and corresponds to a dictionary or associative array in other languages. To take a single value out you write $name{key}.

The sample code builds a hash of products and prices — my %prices = ("apple" => 100, "orange" => 80); — and reads a value out with $prices{"apple"}.

A common early stumble is again the changing sigil: %prices for the hash as a whole, $prices{...} for a value. As with arrays, holding on to "% for the whole thing, $ for one value" makes it click.

In professional work, hashes come up constantly for managing configuration values and for grouping aggregated results by key.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Perl directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)