5.5.3. Discussion
Here's a simple example, iterating through the
%food_color hash from the introduction:
# %food_color per the introduction
while(($food, $color) = each(%food_color)) {
print "$food is $color.\n";
}
Banana is yellow.
Apple is red.
Carrot is orange.
Lemon is yellow.
foreach $food (keys %food_color) {
my $color = $food_color{$food};
print "$food is $color.\n";
}
Banana is yellow.
Apple is red.
Carrot is orange.
Lemon is yellow.
We didn't really need the $color variable in the
foreach example, because we use it only once.
Instead, we could have written:
print "$food is $food_color{$food}.\n"
Every time each is called on the same hash, it
returns the "next" key-value pair. We say "next" because the pairs
are returned in the order the underlying lookup structure imposes on
them, which appears to be no order at all. When
each runs out of hash elements, it returns the
empty list ( ), whose assignment tests false and
terminates the while loop.
The foreach example uses keys,
which constructs an entire list containing every key from the hash
before the loop even begins executing. The advantage to using
each is that it gets the keys and values one pair
at a time. If the hash contains many keys, not having to preconstruct
a complete list of them can save substantial memory. The
each function, however, doesn't let you control
the order in which pairs are processed.
Using foreach and keys to loop
over the list lets you impose an order. For instance, if we wanted to
print the food names in alphabetical order:
foreach $food (sort keys %food_color) {
print "$food is $food_color{$food}.\n";
}
Apple is red.
Banana is yellow.
Carrot is orange.
Lemon is yellow.
while ( ($k,$v) = each %food_color ) {
print "Processing $k\n";
keys %food_color; # goes back to the start of %food_color
}
Modifying a hash while looping over it with each
or foreach is, in general, fraught with danger.
The each function can behave differently with
tie d and untied hashes when you add or delete keys
from a hash. A foreach loops over a pregenerated
list of keys, so once the loop starts, foreach
can't know whether you've added or deleted keys. Keys added in the
body of the loop aren't automatically appended to the list of keys to
loop over, nor are keys deleted by the body of the loop deleted from
this list.