Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a Perl hash contains a key mapping to an undefined value?

I need to determine if a Perl hash has a given key, but that key will be mapped to an undef value. Specifically, the motivation for this is seeing if boolean flags while using getopt() with a hash reference passed into it. I've already searched both this site and google, and exists() and defined() don't seem to be applicable for the situation, they just see if the value for a given key is undefined, they don't check if the hash actually has the key. If I an RTFM here, please point me to the manual that explains this.

like image 443
Alex Marshall Avatar asked Jan 23 '10 22:01

Alex Marshall


People also ask

How do you check if a hash contains a key Perl?

The exists() function in Perl is used to check whether an element in an given array or hash exists or not. This function returns 1 if the desired element is present in the given array or hash else returns 0.

How do I remove a key from a hash in Perl?

undef $hash{$key} and $hash{$key} = undef both make %hash have an entry with key $key and value undef . The delete function is the only way to remove a specific entry from a hash. Once you've deleted a key, it no longer shows up in a keys list or an each iteration, and exists will return false for that key.

How do I check if a Perl hash is empty?

From perldoc perldata: If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash.

How do I add a key to a hash in Perl?

Basic Perl hash "add element" syntax$hash{key} = value; As a concrete example, here is how I add one element (one key/value pair) to a Perl hash named %prices : $prices{'pizza'} = 12.00; In that example, my hash is named %prices , the key I'm adding is the string pizza , and the value I'm adding is 12.00 .


2 Answers

exists() and defined() don't seem to be applicable for the situation, they just see if the value for a given key is undefined, they don't check if the hash actually has the key

Incorrect. That is indeed what defined() does, but exists() does exactly what you want:

my %hash = (
    key1 => 'value',
    key2 => undef,
);

foreach my $key (qw(key1 key2 key3))
{
    print "\$hash{$key} exists: " . (exists $hash{$key} ? "yes" : "no") . "\n";
    print "\$hash{$key} is defined: " . (defined $hash{$key} ? "yes" : "no") . "\n";
}

produces:

$hash{key1} exists: yes
$hash{key1} is defined: yes
$hash{key2} exists: yes
$hash{key2} is defined: no
$hash{key3} exists: no
$hash{key3} is defined: no

The documentation for these two functions is available at the command-line at perldoc -f defined and perldoc -f exists (or read the documentation for all methods at perldoc perlfunc*). The official web documentation is here:

  • http://perldoc.perl.org/functions/exists.html
  • http://perldoc.perl.org/functions/defined.html

*Since you specifically mentioned RTFM and you may not be aware of the locations of the Perl documentation, let me also point out that you can get a full index of all the perldocs at perldoc perl or at http://perldoc.perl.org.

like image 107
Ether Avatar answered Oct 04 '22 23:10

Ether


If I'm reading your question correctly, I think you are confused about exists. From the documentation:

exists EXPR

Given an expression that specifies a hash element or array element, returns true if the specified element in the hash or array has ever been initialized, even if the corresponding value is undefined.

For example:

use strict;
use warnings;

my %h = (
    foo => 1,
    bar => undef,
);

for my $k ( qw(foo bar baz) ){
    print $k, "\n" if exists $h{$k} and not defined $h{$k};
}
like image 32
FMc Avatar answered Oct 04 '22 23:10

FMc