<?xml version="1.0" encoding="windows-1252"?>
<node id="183" title="perlfunc:each" created="1999-08-24 18:41:52" updated="2005-08-12 23:30:57">
<type id="119">
perlfunc</type>
<author id="114">
gods</author>
<data>
<field name="doctext">
</field>
<field name="name">

&lt;P&gt;
each - retrieve the next key/value pair from a hash

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">

&lt;P&gt;
each 
&lt;FONT SIZE=-1&gt;HASH&lt;/FONT&gt;

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="description">

&lt;P&gt;
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 &lt;CODE&gt;&amp;quot;0&amp;quot;&lt;/CODE&gt; or &lt;CODE&gt;&amp;quot;&amp;quot;&lt;/CODE&gt;, which are logically false; you may wish to avoid constructs like &lt;CODE&gt;while ($k = each %foo) {}&lt;/CODE&gt;
for this reason.)

&lt;P&gt;
Entries are returned in an apparently random order. When the hash is entirely read, a null array is returned in list context (which when assigned produces a 
&lt;FONT SIZE=-1&gt;FALSE&lt;/FONT&gt; (
&lt;CODE&gt;0&lt;/CODE&gt;) value), and [perlfunc:undef|undef] in scalar context. The next call to [perlfunc:each|each()] after that will start iterating again. There is a single iterator for each
hash, shared by all [perlfunc:each|each()],
[perlfunc:keys|keys()], and [perlfunc:values|values()] function calls in the program; it can be reset by reading all the elements
from the hash, or by evaluating &lt;CODE&gt;keys HASH&lt;/CODE&gt; or
&lt;CODE&gt;values HASH&lt;/CODE&gt;. 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.

&lt;P&gt;
The following prints out your environment like the 
&lt;CODE&gt;printenv(1)&lt;/CODE&gt; program, only
in a different order:

&lt;P&gt;
&lt;PRE&gt;    while (($key,$value) = each %ENV) {
        print &amp;quot;$key=$value\n&amp;quot;;
    }
&lt;/PRE&gt;
&lt;P&gt;
See also [perlfunc:keys|keys()] and [perlfunc:values|values()].

&lt;HR&gt;
</field>
</data>
</node>
