Reputation: 1211
I want to loop over two hashes somehow so that if the keys of Hash A, equal the values of Hash B, then do something:
eg.
my $hash1 = {
'STRING_ID1' => {default => 'Some string'},
'STRING_ID3' => {default => 'Some string'},
'STRING_ID5' => {default => 'Some string'},
'STRING_ID7' => {default => 'Some string'},
};
my $hash2 = {
content => 'STRING_ID',
content1 => 'Some text that doesn't equal an ID',
content2 => 'STRING_ID_5',
content3 => 'STRING_ID_8',
};
if those values are equal, then I want to call a service which gets me a localized string.
The only way I can think of is:
while (($key, $value) = each (%hash1, %hash2)) {
if ($key eq $value) {
$service->getLocalizedString($key);
}
}
Upvotes: 1
Views: 693
Reputation: 44
As far as I understand the question, you want to check if %hash1 contains an element with a string replacement, for the identifiers listed in the values in %hash2?
while (my ($key,$value) = each(%$hash2)) {
# check if we have an entry in $hash1
if (exists $hash1->{$value}) {
# call service with the STRING_ID as argument
$service->getLocalizedString($value);
} else {
# nothing found
}
}
Upvotes: 1
Reputation: 118695
Since they are hashes, you don't need to use a loop to perform a lookup by key.
while (my ($key2,$value2) = each %$hash2) {
if (exists $hash1->{$value2}) {
print "($key2,$value2) from \$hash2 ";
print "matches ($value2,$hash1->{$value2}) from \$hash1\n";
}
}
Upvotes: 4