in reply to Best Hash Practices?

1. I don't know of any way to cause a hash key to be created by use of an "if" test.

2. I don't know of any way to cause a hash key's value to be "non-existant". There is a thing called "undefined", undef. But undef is not the same as "non-existant". undef means exists but I don't know what the value is.

3. When you test a hash key, you are testing the value of the key. It can be true or false. false values are: "undef","",'',0.

4. If a hash key value "exists" then it can have any one of the 4 values above. Update: well of course, then it can also have some other string or numeric value. The above 4 things, which are actually only 3 things, undef, null string and zero are all the same "false" value in a logical expression.

5. If a hash key value is "defined", then there only 3 possibilities. Update: well "" and '' are the same once the string is interpreted.

#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = ('a' => 2, 'b' => 3, 'c' => undef); print Dumper (\%hash); if (exists ($hash{'c'}) ) {print "key c exists\n"} else {print "key c does not exist\n"}; print Dumper (\%hash); if ( defined ($hash{'c'}) ) {print "key c defined\n"} else {print "key c not defined\n"}; if (exists ($hash{'d'}) ) {print "key d exists\n"} else {print "key d does not exist\n"}; #note that undef,"",'' and 0 all evaluate to "false" #play with c value above and run this code #you can call defined($xxx) to figure out the difference #between a false value from "",'',0 and undef. if (my $x = $hash{'c'}) {print "c has value $x\n"} else {print "c has value,\"\",0 or undef\n"}; if (my $x = $hash{'b'}) {print "b has value $x\n"} else {print "b has no value\n"}; __END__ $VAR1 = { 'c' => undef, 'a' => 2, 'b' => 3 }; key c exists $VAR1 = { 'c' => undef, 'a' => 2, 'b' => 3 }; key c not defined key d does not exist c has no value b has value 3

Replies are listed 'Best First'.
Re^2: Best Hash Practices?
by ikegami (Patriarch) on Oct 09, 2009 at 03:36 UTC

    1. I don't know of any way to cause a hash key to be created by use of an "if" test.

    Correct. There isn't. Another operator has to come into play. However, the following fools many since the operator is invisible:

    if ($hash{foo}{bar})

    See my reply to the OP.

    2. I don't know of any way to cause a hash key's value to be "non-existant".

    delete $hash{foo}; delete @hash{qw( foo bar )}; delete local $hash{foo}; # Since 5.11.0 delete local @hash{qw( foo bar )}; # Since 5.11.0

    When you test a hash key, you are testing the value of the key. It can be true or false. false values are: "undef","",'',0

    The string "undef" isn't false. Plain old undef is, though.

    The second and third literal you posted represent the same value.

    And you're missing some, most notably "0". Except for some insane situations, anything that stringifies to "" or "0" is false. The common false values are undef, the empty string, 0 and "0".

    4. If a hash key value "exists" then it can have any one of the 4 values above.

    Not true. Aside from the fact that you only listed three values, a hash value can be an scalar, not just false ones.

    5. If a hash key value is "defined", then there only 3 possibilities.

    Not true. It can be any scalar value except undef.

    Update: Added lots as I found that every claim after the first had serious errors.

      1. I don't know of any way to cause a hash key to be created by use of an "if" test.

      Correct. There isn't. An other operator has to come into play. However, the following fools many since the operator is invisible:

      if ($hash{foo}{bar}) download

      See my reply to the OP. Interesting...I will have to experiment with this. 2nd hash dimension wasn't part of the question.

      2. I don't know of any way to cause a hash key's value to be "non-existant".

      delete $hash{foo}; delete @hash{qw( foo bar )}; delete local $hash{foo}; # Since 5.11.0 delete local @hash{qw( foo bar )}; # Since 5.11.0
      I'm only on 5.10, so learned something new. Update: still don't see it, ie. how to leave the key but have the value of that key be anything other than undef,string(null or not) or number. To the best of my knowledge a hash key will always evaluate to at least undef. delete $hash{foo} removes key foo and its value.
      When you test a hash key, you are testing the value of the key. It can be true or false. false values are: "undef","",'',0

      The string "undef" isn't false. Plain old undef is, though.
      Yes this was a typo, quotes were wrong to use.

      The second and third literal you posted are the same value.

      And you're missing some. Except for some insane situations, anything that stringifies to "" or "0" is false. The common false values are undef, the empty string, 0 and "0".
      no disagreement here. 0 and "0" I believe will wind up being in practice the same thing.

      4. If a hash key value "exists" then it can have any one of the 4 values above. Not true. Aside from the fact that you only listed three values, a hash value can be an scalar, not just false ones. 5. If a hash key value is "defined", then there only 3 possibilities. Not true. It can be any scalar value except undef.
      I meant the false values, you are correct.
      Update: Added lots as I found that every claim had serious errors.
      perhaps not one of my better posts..posted code works as claimed, but explanation could have been better.

      Thanks for your clarifications.

        To the best of my knowledge a hash key will always evaluate to at least undef.

        You keep using key when you're talking about the value. Either way, I never said otherwise.

        0 and "0" I believe will wind up being in practice the same thing.

        They are different although usually interchangeable. And then there's "0.0". It's true while equal to zero. That's why it's important to list "0"

Re^2: Best Hash Practices?
by gloryhack (Deacon) on Oct 09, 2009 at 02:56 UTC
    1. I don't know of any way to cause a hash key to be created by use of an "if" test.
    if ($hash{foo} = 42) { print "The answer to life, the universe, and everything is contained + within foo!\n"; }
    So the answer to life, the universe, and everything is simply a common error.
      This if ($hash{foo} = 42) is an assignment statement and the if tests the result of that.
       if ($hash{foo} == 42) would probably do something else.

      for all my goofs above, I don't see how a properly formatted, syntactically correct "if" statement, a question in essence, can cause a new element to be entered into a data structure. I can see how this above statement could do that, but that is because it is more than a simple logical "if".