programming-examples/perl/Hash/Is hash key defined and existed.pl

17 lines
366 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
#!/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";
}