In your question you should post an example of the data structure you are working with. Your description says that you are dealing with a hash of arrays, and that for every key there are two values. In some cases those two values are the same, and in other cases they are not. Since you're using substr in your example, I'll assume that "the same" means stringwise equality.
I'm imagining that your datastructure looks like this:
my %hash = (
foo => ['this', 'that'], # Two different values
bar => ['those', 'those'], # Two identical values
baz => ['the', 'other'], # Not the same
buzz => ['same', 'same'], # The same
);
In this structure you would want to print the name of the bar key and buzz key, since the values in the array ref for those keys are the same. You didn't suggest that there could ever be more or less than two values, so I won't solve the problem that isn't stated. Here's an example of how to accomplish what you're after given this data structure:
my %hash = (
foo => ['this', 'that'], # Two different values
bar => ['those', 'those'], # Two identical values
baz => ['the', 'other'], # Not the same
buzz => ['same', 'same'], # The same
);
while (my ($key, $aref) = each %hash) {
if ($aref->[0] eq $aref->[1]) {
print "$key\n";
}
}
The output now should be 'bar' and 'buzz'.
If there could be fewer than two elements to compare, you could do this:
if (2==@$aref && $aref->[0] eq $aref->[1]) ...
If there could be more than two, and you want to assure that they are all equal, you could do this:
while (my ($key, $aref) = each %hash) {
my %seen;
$seen{$_}++ for @$aref;
next if 1 != keys %seen;
print "$key\n";
}
If these scenarios don't match the problem you are trying to solve, please refine your question to remove the ambiguity that has resulted in a diverse selection of answers.
|