Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Deletes a value from a hashTest whether a hash key is present (Berikutnya)
Functions for real %HASHes

Retrieve the next key/value pair from a hash

Daftar Isi

  • each HASH

  • each ARRAY

  • each EXPR

    When called on a hash in list context, returns a 2-element listconsisting of the key and value for the next element of a hash. In Perl5.12 and later only, it will also return the index and value for the nextelement of an array so that you can iterate over it; older Perls considerthis a syntax error. When called in scalar context, returns only the key(not the value) in a hash, or the index in an array.

    Hash entries are returned in an apparently random order. The actual randomorder is subject to change in future versions of Perl, but it isguaranteed to be in the same order as either the keys or valuesfunction would produce on the same (unmodified) hash. Since Perl5.8.2 the ordering can be different even between different runs of Perlfor security reasons (see Algorithmic Complexity Attacks in perlsec).

    After each has returned all entries from the hash or array, the nextcall to each returns the empty list in list context and undef inscalar context; the next call following that one restarts iteration.Each hash or array has its own internal iterator, accessed by each,keys, and values. The iterator is implicitly reset when each hasreached the end as just described; it can be explicitly reset by callingkeys or values on the hash or array. If you add or delete a hash'selements while iterating over it, entries may be skipped or duplicated--sodon't do that. Exception: In the current implementation, it is always safeto delete the item most recently returned by each(), so the followingcode works properly:

    1. while (($key, $value) = each %hash) {
    2. print $key, "\n";
    3. delete $hash{$key}; # This is safe
    4. }

    This prints out your environment like the printenv(1) program,but in a different order:

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

    Starting with Perl 5.14, each can take a scalar EXPR, which must holdreference to an unblessed hash or array. The argument will be dereferencedautomatically. This aspect of each is considered highly experimental.The exact behaviour may change in a future version of Perl.

    1. while (($key,$value) = each $hashref) { ... }

    To avoid confusing would-be users of your code who are running earlierversions of Perl with mysterious syntax errors, put this sort of thing atthe top of your file to signal that your code will work only on Perls ofa recent vintage:

    1. use 5.012;# so keys/values/each work on arrays
    2. use 5.014;# so keys/values/each work on scalars (experimental)

    See also keys, values, and sort.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Deletes a value from a hashTest whether a hash key is present (Berikutnya)