You can only store scalar values in a hash. References, however, are scalars. This solves the problem of storing multiple values for one key by making
$hash{$key}
a reference to an array containing values for
$key
. The normal hash operations - insertion, deletion, iteration, and testing for existence - can now be written in terms of array operations like
push
,
splice
, and
foreach
.

This code shows simple insertion into the hash. It processes the output of
who
(1) on Unix machines and outputs a terse listing of users and the ttys they're logged in on:

The heart of the code is the
push
line, the multihash version of
$ttys{$user}
=
$tty
. We interpolate all the tty names in the
print
line with
@{$ttys{$user}}
. We'd loop over the anonymous array if, for instance, we wanted to print the owner of each tty:

The
exists
function can have two meanings: "Is there at least one value for this key?" and "Does this value exist for this key?" Implementing the second approach requires searching the array for the value. The
delete
function and the first sense of
exists
are interrelated: If we can guarantee that no anonymous array is ever empty, we can use the built-in
exists
. We ensure that no anonymous array is ever empty by checking for such a situation after deleting an element: