senduran has asked for the wisdom of the Perl Monks concerning the following question:
Why doesn't this work as expected? The contents of $hash seem to disappear after the first loop:
my $hash = { one => 1 }; PRED: foreach my $i (0..5) { print "PRED $i\n"; while (my ($key, $val) = each %{$hash}) { print " - $key => $val\n"; next PRED; } exit; }
But this does? Doing some operation on $hash keeps the content alive:
my $hash = { one => 1 }; PRED: foreach my $i (0..5) { scalar(keys %{$hash}); print "PRED $i\n"; while (my ($key, $val) = each %{$hash}) { print " - $key => $val\n"; next PRED; } exit; }
I don't get it. Can someone explain what is going on?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What's happening to this hash?
by GrandFather (Saint) on Jun 07, 2007 at 09:40 UTC | |
|
Re: What's happening to this hash?
by varian (Chaplain) on Jun 07, 2007 at 09:46 UTC |