Satar Vafapoor wrote:
>
> Hi all,
Hello,
> When I run the following code(with any hash), I get a contiuous stream
> of output. The presence of the array in the loop causes the contiuous
> stream of output. When the array is commented out an output is generated.
> Why does the presence of the array cause a contiuous stream of output.
> Thanks
>
> while (($k,$v)= each %ENV)
> {
> @ks=keys %ENV;
> print "$k $v\n";
> }
perldoc -f each
[snip]
When the hash is entirely read, a null array is
returned in list context (which when assigned
produces a false (`0') value), and `undef' in
scalar context. The next call to `each' after
that will start iterating again. There is a
^^^^^^^^^^
single iterator for each hash, shared by all
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`each', `keys', and `values' function calls in the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
program; it can be reset by reading all the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
elements from the hash, or by evaluating `keys
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
HASH' or `values 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.
This means that when you use "keys %ENV" it resets the iterator that
"each %ENV" is using and each() will always read the first key and value
from %ENV.
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]