P
PerlFAQ Server
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
4.71: How can I check if a key exists in a multilevel hash?
(contributed by brian d foy)
The trick to this problem is avoiding accidental autovivification. If
you want to check three keys deep, you might naïvely try this:
my %hash;
if( exists $hash{key1}{key2}{key3} ) {
...;
}
Even though you started with a completely empty hash, after that call to
"exists" you've created the structure you needed to check for "key3":
%hash = (
'key1' => {
'key2' => {}
}
);
That's autovivification. You can get around this in a few ways. The
easiest way is to just turn it off. The lexical "autovivification"
pragma is available on CPAN. Now you don't add to the hash:
{
no autovivification;
my %hash;
if( exists $hash{key1}{key2}{key3} ) {
...;
}
}
The "Data:iver" module on CPAN can do it for you too. Its "Dive"
subroutine can tell you not only if the keys exist but also get the
value:
use Data:iver qw(Dive);
my @exists = Dive( \%hash, qw(key1 key2 key3) );
if( ! @exists ) {
...; # keys do not exist
}
elsif( ! defined $exists[0] ) {
...; # keys exist but value is undef
}
You can easily do this yourself too by checking each level of the hash
before you move onto the next level. This is essentially what
"Data:iver" does for you:
if( check_hash( \%hash, qw(key1 key2 key3) ) ) {
...;
}
sub check_hash {
my( $hash, @keys ) = @_;
return unless @keys;
foreach my $key ( @keys ) {
return unless eval { exists $hash->{$key} };
$hash = $hash->{$key};
}
return 1;
}
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
4.71: How can I check if a key exists in a multilevel hash?
(contributed by brian d foy)
The trick to this problem is avoiding accidental autovivification. If
you want to check three keys deep, you might naïvely try this:
my %hash;
if( exists $hash{key1}{key2}{key3} ) {
...;
}
Even though you started with a completely empty hash, after that call to
"exists" you've created the structure you needed to check for "key3":
%hash = (
'key1' => {
'key2' => {}
}
);
That's autovivification. You can get around this in a few ways. The
easiest way is to just turn it off. The lexical "autovivification"
pragma is available on CPAN. Now you don't add to the hash:
{
no autovivification;
my %hash;
if( exists $hash{key1}{key2}{key3} ) {
...;
}
}
The "Data:iver" module on CPAN can do it for you too. Its "Dive"
subroutine can tell you not only if the keys exist but also get the
value:
use Data:iver qw(Dive);
my @exists = Dive( \%hash, qw(key1 key2 key3) );
if( ! @exists ) {
...; # keys do not exist
}
elsif( ! defined $exists[0] ) {
...; # keys exist but value is undef
}
You can easily do this yourself too by checking each level of the hash
before you move onto the next level. This is essentially what
"Data:iver" does for you:
if( check_hash( \%hash, qw(key1 key2 key3) ) ) {
...;
}
sub check_hash {
my( $hash, @keys ) = @_;
return unless @keys;
foreach my $key ( @keys ) {
return unless eval { exists $hash->{$key} };
$hash = $hash->{$key};
}
return 1;
}
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.