Hi, I just joined this list, and I thought I'd chime in.  I think
Mr. Foy's proposed change is a good improvement, but it introduces an
ambiguity in my opinion which I'd like to resolve...

[EMAIL PROTECTED] (_brian_d_foy) writes:
> -The keys() function also resets the iterator, which in void context is
> -faster for tied hashes than would be iterating through the whole 
> -hash, one key-value pair at a time.
> +The keys() function also resets the iterator, which means that you may 
> +see strange results if you use this between uses of other hash operators 
> +such as each().

What else besides each() is affected?  values() has the same property,
I believe, but it isn't affected by calling keys().  I would rewrite
this as:

+The keys() function also resets the iterator, which means that you may 
+see strange results if you use this between uses of the each()
+operator on the same hash, such as inside a while loop that iterates
+over the hash.

Note the words "on the same hash"; I think that is important to
mention.  Consider the following program:

my %a = (alpha => 1, beta => 2, gamma => 3);
my %b = (reverse %a);

while((my $greek, $num) = each %a) {
    print "$greek: $num\n";
    print join(" ", sort values %b), "\n";
    print join(" ", sort keys %b), "\n";
}

The iterator on %a is not disturbed by the use of keys(%b) or
values(%b) inside the loop.  But change %b to %a, and it loops
forever, as expected.

--Bill.

-- 
William R Ward            [EMAIL PROTECTED]          http://www.wards.net/~bill/
-----------------------------------------------------------------------------
     If you're not part of the solution, you're part of the precipitate.

Reply via email to