each 
eachHASHeachARRAYeachEXPR
This function steps through a hash one key/value pair at a
time. When called in list context, each returns a two-element list consisting of
the key and value for the next element of a hash so that you can iterate
over it. When called in scalar context, each returns just the key for the next element
in the hash. When the hash is entirely read, the empty list is returned,
which when assigned produces a false value in scalar context, such as a
loop test. The next call to each
after that will start iterating again. The typical use is as follows,
using predefined %ENV hash:
while (($key,$value) = each %ENV) {
say "$key=$value";
}Internally, a hash maintains its own entries in an apparently
random order. The each function
iterates through this sequence because every hash remembers which entry
was last returned. The actual ordering of this sequence is subject to
change in future versions of Perl, but is guaranteed to be in the same
order as the keys (or values) function would produce on the same
(unmodified) hash. For security reasons, this ordering can vary between
different runs of the same program.
Perl maintains a single iterator for each hash, shared by all
each, keys, and values function calls in the program; it can
be reset by reading all the elements from the hash, or by evaluating
keys %hash or values %hash. If you add or delete elements of a hash ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access