I have an hash map like this
my $name = 'AUS'; #dynamic values
my %hash = { 'a'=>{
'x'=> {
'1' =>'US'
'2' =>'UK'
}
'y'=>{
'1' =>'AFRICA'
'2' =>'AUS'
}
}
'b'=>{
'x' =>{
'1' =>'US'
'2' =>'UK'
}
}
};
I am trying to find whether name is unique in the hash for each column
foreach my $key(keys %hash)
{
if($name ne $hash{}{}{}) #is name unique in whole hash?
{
print "something";
}
else
{
print "nothing";
}
}
All is fine but when it comes to key 'b' it checks that AUS is not present and prints "something" but I want it to check the 'a' key too to see if has 'AUS' value. So,how to check whether $name exists in whole hash (i can't use find via key-value pair since i am trying to find and print in each column) ?
There's no magic bullet here. You have to traverse your hash and inspect each value. There's a variety of approaches to doing this, and which you use is rather dependent on how your hash-source gets populated.
A recursive solution would be:
Because this is recursive, it will walk to any 'depth' of the hash, but that might not be entirely appropriate for you use-case.
However the fact that you're talking about columns, suggests to me that this hash is being populated from elsewhere - I would suggest you look at that population process, because it's quite likely that's a better place to start picking out particular counts-of-values.
If you need a more versatile lookup table:
if I understand correctly you want something like this: