I've been dealing with hashes and references a lot lately, and I wrote a test script to re-educate myself on the differences between hash keys existing, being defined, and containing data...I thought I'd post it cause it's interesting, albiet far from news.

So, I declare a hash, and work with four keys:

and the code, of course:
#!/usr/bin/perl -w use strict; my %hash; $hash{'a'} = "alpha"; $hash{'b'} = ""; $hash{'c'} = undef; print "a='" . $hash{'a'} . "'\n"; print "b='" . $hash{'b'} . "'\n"; print "c='" . $hash{'c'} . "'\n"; print "d='" . $hash{'d'} . "'\n\n"; print "exists(a)='" . exists($hash{'a'}) . "'\n"; print "exists(b)='" . exists($hash{'b'}) . "'\n"; print "exists(c)='" . exists($hash{'c'}) . "'\n"; print "exists(d)='" . exists($hash{'d'}) . "'\n\n"; print "defined(a)='" . defined($hash{'a'}) . "'\n"; print "defined(b)='" . defined($hash{'b'}) . "'\n"; print "defined(c)='" . defined($hash{'c'}) . "'\n"; print "defined(d)='" . defined($hash{'d'}) . "'\n\n"; print "a is true\n" if($hash{'a'}); print "b is true\n" if($hash{'b'}); print "c is true\n" if($hash{'c'}); print "d is true\n" if($hash{'d'});
so, as we can see from the output of this, a key can be totally undeclared (not exist), can be declared but undefined (equals undef), OR be declared AND defined yet empty (equals an empty string), all in addition to actually having a populated value. and THEN we can look at which ones evaluate to being true :o

i know that this is one of those aspects of perl that scares away sooo many newcomers, and its a shame. because even though perl can confuse a 5+ year user like myself so easily, its complexity is exactly what makes it so versatile.

so while many here already understand everything said, perhaps a few newcomers will read this and walk away not so frightened by hashes and keys and values, oh my!

edit: rearranged the code to make a bit more sense, props to ikegami for pointing it out

__________
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
- Terry Pratchett


In reply to "A meditation on Hashes", or "Why i need more aspirin" by EvanK

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.