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...

In reply to Re: Re: Unexpected persistence with each hash by demerphq
in thread Unexpected persistence with each hash by Zaxo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.