in reply to Re^5: how to identify a null hash value
in thread how to identify a null hash value
In addition to stevieb's correct answer, I'll add: there's no such thing as "NULL" in Perl, so that may cause some confusion. Values in Perl may be defined or undefined, as well as true or false. But references (pointers to other things) are always defined and true, even if the thing they point to is empty, as in the case of a hash with no keys and values.
#!/usr/bin/env perl use 5.010; use warnings; use strict; my %hash = (); # create empty hash my $ref = \%hash; # create reference pointing to empty hash if($ref){ say 'Reference is true'; # <- always true } else { say 'Reference is false'; } if (%$ref){ # derefence hash and check the number of elements in it say 'Hash has elements'; } else { say 'Hash is empty'; # <-- hash has no elements } $hash{a} = 1; # add a key and value to hash if (%$ref){ # derefence hash and check the number of elements in it say 'Hash has elements'; # <-- now it has one key and one value } else { say 'Hash is empty'; }
Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: how to identify a null hash value
by smartyollie (Novice) on Jun 04, 2015 at 23:16 UTC | |
by aaron_baugher (Curate) on Jun 05, 2015 at 01:34 UTC |