411 REFERENCE · Wikibooks
Perl Programming/Keywords/each
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Perl dump else Keywords
The each keyword
each returns a two-element list consisting of the key and value for the next element of a hash, if called on a hash in list context. In Perl 5.12 and later only, it returns the index and value for the next element of an array. For older Perl versions, this is considered as a syntax error. When called in scalar context, it returns only the key (not the value) in a hash, or the index in an array.
Syntax
each ARRAY
each EXPRESSION
each HASH
Examples
The code
while (($key,$value) = each %ENV) {
print "$key = $value\n";
}
returns the environment as key-value pairs, separated by an assignment sign, like: USERPROFILE = C:\Users\<user name>
USERDNSDOMAIN = <domain name>
[…]
HOMEDRIVE = U:
PATH = C:\Programms\Perl\site\bin;…
[…]
SYSTEMDRIVE = C:
[…]
USERNAME = <user name>
[…]
See also
Conditionals
Perl dump else Keywords