smart match operator for hash of hash

一笑奈何 提交于 2019-12-24 03:12:41

问题


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


回答1:


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




回答2:


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



回答3:


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.




回答4:


Most likely, your bug is here:

foreach $characters (keys %{$HoH{$cluster}}) {

which should read

foreach $characters (keys %{$HoH{$value}}) {

. Probably.



来源:https://stackoverflow.com/questions/12423791/smart-match-operator-for-hash-of-hash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!