# always use strictures (the following two lines)
use strict;
use warnings;
my @array = qw(key1 key2 key3 key4);
my %hash = (key2 => undef, key4 => 'The goods', key5 => 'More goods');
doLoop ();
sub doLoop {
for my $key (@array) {
next if ! exists $hash{$key}; # Skip if the key is not used in the hash
next if ! defined $hash{$key}; # Skip if the key exists, but is undefined
next if $hash{$key} ne 'No goods';
print "The value for '$key' is '$hash{$key}'\n";
return;
}
print "Failed to find matching key\n"
}
####
Failed to find matching key
##
##
use strict;
use warnings;
my @array = qw(key1 key2 key3 key4);
my %hash = (key2 => undef, key4 => 'The goods', key5 => 'More goods');
my $match = 0;
for my $key (@array) {
next if ! exists $hash{$key}; # Skip if the key is not used in the hash
next if ! defined $hash{$key}; # Skip if the key exists, but is undefined
next if $hash{$key} ne 'No goods';
print "The value for '$key' is '$hash{$key}'\n";
$match = 1;
last;
}
print "Failed to find matching key\n" if ! $match;