Zaxo has asked for the wisdom of the Perl Monks concerning the following question:

I was tinkering with item-by-item deletion of hashes, looking for something yet unsaid about Hash Entry Deallocation. Investigating the form while (%hash) {delete...}, I found behavior I hadn't expected. The seemingly sane,

my %foo; @foo{qw/ foo bar baz /} = 1 .. 3; while (%foo) { my $key = each %val; my $val = delete $foo{$key}; printf "%s => %s is gone.\n", $key, $val; }
continues to spin after the hash is depleted, endlessly printing ' => is done.'.

The ugly variation,

my %foo; @foo{qw/ foo bar baz /} = 1 .. 3; my @keys = keys %foo; while (%foo) { my $key = shift @keys; my $val = delete $foo{$key}; printf "%s => %s is gone.\n", $key, $val; }
works as expected.

I first suspected that modifying the hash upset each's bookkeeping, but then how could the more conventional,

while (my ($key, $val) = each %hash) { delete $hash{$key}; printf "%s => %s is gone.\n", $key, $val; }
work?

Does anyone know why this happens?

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Unexpected persistence with each hash
by dragonchild (Archbishop) on Jul 01, 2003 at 03:43 UTC
    The seemingly sane version could use some strictness. my $key = each %val makes no sense. The slightly more sane my $key = each %foo still makes no sense. each returns a list. $key is now always 2, unless it's 0.

    Oh - try seeing what the stringification of %foo is, even when it's empty. It's not what you think. (And, even though overload has a hook for stringy, nummy, and booly - I'm pretty positive that scalars, arrays, and hashes all use the same values for each hook.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      each returns a list. $key is now always 2, unless it's 0.

      Not so. each is context sensitive as are most things in Perl (you knew that, silly :-)

      each HASH
      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 only the key for the next element in the hash.

      Actually there is an interesting point here, iirc when you do a Tie::Hash implementation the EACH() sub only returns the key. Perl then automatically follows with a FETCH() to get the value (and I think it does so regardless of the context as well that the each keyword is used in). This is a bit annoying as often in this situation you need to do the same work to get the key or the value and finding one gives you the other. (You can play games to optimise this but its still annoying.)

      Oh - try seeing what the stringification of %foo is, even when it's empty. It's not what you think.

      I would have thought it would be false when the hash is empty, and suprise suprise it is. :-)

      The only change Zaxo needed to make to his code was as you pointed out to change the %val to %foo. Your points about strictures etc is good of course :-)

      my %foo; @foo{qw/ foo bar baz /} = 1 .. 3; printf "%%foo = %s { %s } = ( %s )\n",\%foo,scalar %foo, join(",",map "$_ => $foo{$_}",keys %foo); while (%foo) { my $key = each %foo; my $val = delete $foo{$key}; printf "%s => %s is gone.\n", $key, $val; printf "%%foo = %s { %s } = ( %s )\n",\%foo,scalar %foo, join(",",map "$_ => $foo{$_}",keys %foo); }
      %foo = HASH(0x1ab298c) { 2/8 } = ( foo => 1,baz => 3,bar => 2 ) foo => 1 is gone. %foo = HASH(0x1ab298c) { 1/8 } = ( baz => 3,bar => 2 ) baz => 3 is gone. %foo = HASH(0x1ab298c) { 1/8 } = ( bar => 2 ) bar => 2 is gone. %foo = HASH(0x1ab298c) { 0 } = ( )

      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

      Thanks, stricture would have indeed caught the typo. As you suggest, my $key = each %foo; was what I meant. The corrected code,

      my %foo; @foo{qw/foo bar baz/} = 1 .. 3; while (%foo) { my $key = each %foo; my $val = delete $foo($key}; printf "%s => %s is gone.\n", $key, $val; }
      works as intended.

      In scalar context, each returns only a key.

      /me claims the brown bag for today. What you refuse to see, is your worst trap in action. Thanks also to ++Albannach.

      After Compline,
      Zaxo

Re: Unexpected persistence with each hash
by bwana147 (Pilgrim) on Jul 01, 2003 at 09:45 UTC
    Er, what about:
    foreach ( keys %foo ) { printf "%s => %s is gone.\n", $_, delete $foo{$_}; }

    --bwana147