I want to match keys of a hash of hash with regexp .
$line=" Cluster(A,B):A(T) M(S)";
$reg="Cluster";
my ( $cluster, $characters ) = split (/:/,$line);
$HoH{$cluster}={split /[( )]+/,$characters } ;
foreach $value(keys %HoH){
foreach $characters (keys %{$HoH{$cluster}}){
print "$value:$characters\n" if /$reg/ ~~ %HoH;
}
}
now Output is :
Cluster(A,B):A
Cluster(A,B):M
This code is works fine with this sample data,but not with real data!! my data is more complicated but the structure is the same I was wondering if there is some other ways to do this
Perhaps you want just
print "something\n" if exists $HoH{regexp}
or maybe
print "something\n" if grep /regexp/, keys %HoH
but if neither of these are correct then you need to explain better what you need, and give some examples
This is under documented, and I don't grok exactly what the issue is, but the smart match operator works better with references to arrays and hashes. So you may have better luck with
/$reg/ ~~ \%Hoh
SmartMatch is currently complicated, unwieldy and surprising. Don't use it, at least not now. There's talk by the main developers of perl to either greatly simplify it or remove it completely. Either way it won't do what you're asking it to do in the future, so don't rely on it doing that now.
Being more explicit about what you want is better anyway.
Most likely, your bug is here:
foreach $characters (keys %{$HoH{$cluster}}) {
which should read
foreach $characters (keys %{$HoH{$value}}) {
. Probably.