Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

each

Returns the next key-value pair from a hash’s internal iterator.

Advances the hash’s internal iterator and returns the next entry. In list context, pushes a (key, value) pair. In scalar context, returns only the key. When the iterator is exhausted, returns an empty list (list context) or undef (scalar context), and the iterator resets automatically for the next traversal.

Note: keys and values reset the same internal iterator, so calling them during an each loop will restart iteration.

Synopsis

while (my ($k, $v) = each %hash) { ... }
while (my $k = each %hash) { ... }    # scalar context: key only

See Also

keys, values, exists, delete