So, I declare a hash, and work with four keys:
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#!/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'});
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |