当我在 hashref 中查找键和值时,我有以下函数应该返回 true 或 false。我确定我错过了一些东西,但是什么?
如果找到我们搜索的键值字符串,该函数应该返回 true 或 false。
#!/usr/bin/perl
use warnings;
use strict;
my $structure = {
Key1 => {
Key4 => "findme",
Key5 => 9,
Key6 => 10,
},
Key2 => {
Key7 => "abc",
Key8 => 9,
},
};
sub lookup_key_value
{
# Arguments are a hash ref and a list of keys to find
my($hash,$findkey, $findvalue) = @_;
# Loop over the keys in the hash
foreach my $hashkey ( keys %{$hash})
{
# Get the value for the current key
my $value = $hash->{$hashkey};
# See if the value is a hash reference
if (ref($value) eq 'HASH')
{
# If it is call this function for that hash
&lookup_key_value($value,$findkey,$findvalue);
}
if ( ($findkey =~ m/^$hashkey$/) && ( $value =~ m/^$findvalue$/) )
{
print "$findkey = $hashkey, $value = $findvalue\n";
return (0);
}
}
return (1);
}
if ( &lookup_key_value($structure,"Key7","abcX") )
{
print "FOUND !\n";
} else {
print "MISSING !\n";
}