Movatterモバイル変換


[0]ホーム

URL:


functions /each
(source,CPAN)
You are viewing the version of this documentation from Perl 5.005_03.View the latest version
#each HASH

When called in list context, returns a 2-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, returns the key for only the "next" element in the hash. (Note: Keys may be"0" or"", which are logically false; you may wish to avoid constructs likewhile ($k = each %foo) {} for this reason.)

Entries are returned in an apparently random order. The actual random order is subject to change in future versions of perl, but it is guaranteed to be in the same order as either thekeys() orvalues() function would produce on the same (unmodified) hash.

When the hash is entirely read, a null array is returned in list context (which when assigned produces a FALSE (0) value), andundef in scalar context. The next call toeach() after that will start iterating again. There is a single iterator for each hash, shared by alleach(),keys(), andvalues() function calls in the program; it can be reset by reading all the elements from the hash, or by evaluatingkeys HASH orvalues HASH. If you add or delete elements of a hash while you're iterating over it, you may get entries skipped or duplicated, so don't.

The following prints out your environment like the printenv(1) program, only in a different order:

    while (($key,$value) = each %ENV) {print "$key=$value\n";    }

See alsokeys(),values() andsort().

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2025 Movatter.jp