## Hashes

### Iterating over all elements of a hash using
the *each* function

The **each** function returns a
key-value pair as a two-element list.
On each evaluation of this function for the same hash, the next
successive key-value pair is returned until all the elements have been
accessed. When there are no more pairs, *each* returns an empty list.

**Example - demonstrating how the ***each* function works:

%prices = ("shirt" => 45,
"pullover" => 90,
"trousers" => 120,
"socks" => 15);
@a = **each (**%prices**)**; # @a is now (pullover, 90)
@b = **each (**%prices**)**; # @b is now (shirt, 45)
@c = **each (**%prices**)**; # @c is now (socks, 15)
@d = **each (**%prices**)**; # @d is now (trousers, 120)
@e = **each (**%prices**)**; # @e is now ();

**Note:**

Instead of

`@a = each (%prices);`

you may write:

`($item, $shekel) = each (%prices);`

and then `$item` will be `"pullover"` and `$shekel` will be `90`.
**Example - printing all keys and values of the %prices hash:**

#!/usr/local/bin/perl
use strict;
use warnings;
my (%prices, $item, $shekel);
%prices = ("shirt" => 45,
"pullover" => 90,
"trousers" => 120,
"socks" => 15);
while (($item, $shekel) = **each (**%prices**)**) {
print "$item : $shekel NIS\n";
}

Result:
pullover : 90 NIS
shirt : 45 NIS
socks : 15 NIS
trousers : 120 NIS

Table of Contents.

Next.