简体   繁体   English

如何在Perl中仅取消引用数组散列的散列?

[英]How to dereference only hash of hash of array in Perl?

{    
    'A' =>      ['B'], //hash of array   
    'C' => {'D' => [  'E']} //hash of hash of array    
}

When try to parse hash of hash of array.当尝试解析数组散列的散列时。 getting "Not a HASH reference" error.收到“不是哈希引用”错误。 Even I tried used exists and defined keywords to avoid this error.即使我尝试使用exists和defined关键字来避免这个错误。 But result is same error.但结果是同样的错误。

From above i need to only iterate hash of hash of array.从上面我只需要迭代数组散列的散列。

foreach my $keys (keys %$hash){
        print "$keys";
     if (defined $hash->{$keys}->{maptype}){
            foreach my $array_element ( @{$hash->{$keys}->{'D'}} ) {
                        print "$array_element"); 
            }
        }
    }

Not sure if this is best practice, but:不确定这是否是最佳实践,但是:

my $hash = { A => ['B'], C => {D => ['E'], F => [qw(G H I)]}, J=>42 };

for my $key (keys %$hash) {
    if (ref $hash->{$key} eq 'HASH') {
        for my $subkey (keys %{$hash->{$key}}) {
            say "$key => $subkey => [", join(",", @{$hash->{$key}{$subkey}}), "]";
        }
    }
}

outputs产出

C => D => [E]
C => F => [G,H,I]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM