Is hash key defined and existed : key « Hash « Perl






Is hash key defined and existed

   
#!/usr/bin/perl

use strict;
use warnings;

my %hash = ('Key1' => 'Value1', 'Key2' => 'Value2');
my $key = 'Key1';

if (exists $hash{$key}) {
    if (defined $hash{$key}) {
        print "$key exists and is defined as $hash{$key} \n";
    } else {
        print "$key exists but is not defined \n";
    }
} else {
    print "$key does not exist\n";
}

   
    
    
  








Related examples in the same category

1.Determine if the value for the key is defined
2.Hash Key and Value Retrieval
3.Is a key defined in a hash
4.Is a key in a hash
5.Using variable as the key to get the value stored in a hash