in reply to Best Multidimensional Hash Practices?
I read as far as your premise. It's wrong. To test if a scalar has been set use:
if (defined $scalar) {
To test if a hash key/value pair exists use:
if (exists $hash{key}) {
To test if the scalar content of a hash value has been set use:
if (defined $hash{key}) {
Those last two steps can be combined:
if (exists $hash{key} && defined $hash{key}) {
and of course you can make the same tests with a multi-dimensional hash:
if (exists $hash{key1}{key2} && defined $hash{key1}{key2}) {
This last example is interesting if key1 didn't exist - it pops into existence. This is a process called autovivification and happens when Perl needs to have a hash or array element in order to write to it or use it to access an element is references.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Best Multidimensional Hash Practices?
by almut (Canon) on Oct 12, 2009 at 20:06 UTC | |
by CountZero (Bishop) on Oct 12, 2009 at 21:10 UTC | |
by almut (Canon) on Oct 12, 2009 at 21:18 UTC | |
by moritz (Cardinal) on Oct 13, 2009 at 15:14 UTC | |
by muba (Priest) on Oct 13, 2009 at 00:08 UTC | |
by almut (Canon) on Oct 13, 2009 at 01:18 UTC | |
by muba (Priest) on Oct 13, 2009 at 02:22 UTC |