FreeKB - Perl Hash (determine if hash key is defined)
Perl - Hash (determine if hash key is defined)

Let's say you have the following hash. In this example, the hash contains a single key (employee_key).

my %hash = ( 'employee_key' => '' );

 

An if statement can be used to determine if a key exists.

if (exists $hash{employee_key}) {
  print "hash contains 'employee_key' \n";
}
else {
  print "hash does not contain 'employee_key' \n";
}

 

Multidimensional

In this example, name_key is below employee_key.

my %hash = ( 'employee_key' => { 'name_key' => '' } );

 

An if statement can be used to determine if a key exists.

if (exists $hash{employee_key}->{name_key}) {
  print "hash contains 'employee_key -> name_key' \n";
}
else {
  print "hash does not contain 'employee_key -> name_key' \n";
}


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 2a90d in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |