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

In reply to Re: Best Hash Practices? by Marshall
in thread Best Hash Practices? by DamianKaelGreen

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.