Pilot error, this:
for (($key, $value) = each %hash)
Should be:
while (($key, $value) = each %hash)
-Wayne
�
-----Original Message-----
From: Rick Zemer [mailto:[EMAIL PROTECTED]
Sent: Wednesday, September 01, 2004 8:00 AM
To: [EMAIL PROTECTED]
Subject: each vs. keys in hash interation
Greetings all,
I have a question/issue with the hash iterators 'each' and 'keys'.� It
appears that the 'each' method fails in a trivial case, but 'keys' works.�
That is, I have a hash with two keys: "fred" and "wilma".� When I try to
dump the contents of the hash using 'each' it prints one key twice (more
later), whereas when I use 'keys' it prints both keys once.
�
Is this a bug or pilot error?
�
Here is the test code:
use strict;
my (%hash) = ();
my ($key, $value);
{
�$key = "fred";� $value = 1;
�$hash{$key} = $value;
�$key = "wilma"; $value = 2;
�$hash{$key} = $value;
�for (($key, $value) = each %hash)
�{
���� print "key = $key value = $value \n";
�}
�foreach $key (keys %hash)
�{
������ print "key = $key� and value = $hash{$key} \n";
�}
}
The potentially interesting part is that when I run it on Solaris (perl v.
5.005_03), it prints out this:
key = fred value = 1
key = fred value = 1
key = fred and value = 1
key = wilma and value = 2
�
but when I run in on cygwin (perl v5.8.5)
it prints:
key = wilma value = 2
key = wilma value = 2
key = wilma and value = 2
key = fred and value = 1
�
help, thoughts, corrections welcome.
�
-rz.
�
�
�
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs