Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: multiple values for one key in hash

by wade (Pilgrim)
on May 12, 2008 at 22:46 UTC ( #686163=note: print w/ replies, xml ) Need Help??


in reply to Re^2: multiple values for one key in hash
in thread multiple values for one key in hash

Son of a gun! You can just push the value. I could have sworn that this didn't work when I tried it at my old job (about 2 years ago). Is this a feature that has happened since then or have I been brain dead all this time?

Thanks, moritz; ++

--
Wade


Comment on Re^3: multiple values for one key in hash
Re^4: multiple values for one key in hash
by dragonchild (Archbishop) on May 12, 2008 at 23:04 UTC
    It doesn't work if there was an undef explicitly assigned to the value before. A lot of people get tripped up by this because they just assign undef to something they don't use anymore instead of deleting the key.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      How would autovivification know that the undef in lvalue context it's autovivifying wasn't the original undef?

      $ perl -MYAML::Syck -le '$h{bar}=1;$h{bar}=undef;push @{$h{bar}}, qw/a + b c/;print Dump( \%h )' --- bar: - a - b - c

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        huh. My understanding was always that an exists check was done in the background (reflecting my experience with the tied madness that is DBM::Deep), but I've never felt curious enough to explore more deeply.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        How would autovivification know that the undef in lvalue context it's autovivifying wasn't the original undef?

        By using a proxy object that acts as undef. Note that you need a reference for autovivification anyway because push @{...} has to modify something that is stored in place of ... anyway.

        I don't know if Perl 5 does it that way, but I think kp6 does.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://686163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-10-24 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (132 votes), past polls