从多级哈希中提取数据

Extracting data from a multi-level hash

我正在获取以下数据结构作为网络服务调用的响应。

my $triggers1 = $zabbix->raw('trigger','get', $options1);
print Dumper($triggers1);

输出

          $VAR1 = {
                   '10305122' => {
                      'hosts' => [
                                   {
                                     'name' => 'pc4b12cf254444',
                                     'maintenance_type' => '0',
                                     'hostid' => '19295'
                                   }
                                 ],
                      'priority' => '1',
                      'status' => '0',
                      'dependencies' => [],
                      'templateid' => '9892568',
                      'comments' => '',
                      'state' => '0',
                      'triggerid' => '10305122',
                      'expression' => '{14127122}=0',
                      'error' => '',
                      'url' => '',
                      'flags' => '0',
                      'value' => '0',


                    }


           324234' => {
                      'hosts' => [
                                   {
                                     'name' => 'pc45657ba34gy0423',
                                     'maintenance_type' => '0',
                                     'hostid' => '19439'
                                   }
                                 ],
                      'priority' => '1',
                      'status' => '0',
                      'dependencies' => [],
                      'templateid' => '9896452',
                      'comments' => '',
                      'state' => '0',

                      'triggerid' => '10324234',
                      'expression' => '{14167689}=0',
                      'error' => '',
                      'url' => '',
                      'flags' => '0',
                      'value' => '0',
                      'value_flags' => '0',
                      'lastchange' => '1420266068',
                      'type' => '0'

    };

等等

有多个相似的记录

从这个输出中,我想打印 'name' 和 'value' 的值。

如何使用 Perl 打印此文件?

怎么样:

    my  $triggers1 = {
               '10305122' => {
                  'hosts' => [
                               {
                                 'name' => 'pc4b12cf254444',
                                 'maintenance_type' => '0',
                                 'hostid' => '19295'
                               }
                             ],
                  'priority' => '1',
                  'status' => '0',
                  'dependencies' => [],
                  'templateid' => '9892568',
                  'comments' => '',
                  'state' => '0',
                  'triggerid' => '10305122',
                  'expression' => '{14127122}=0',
                  'error' => '',
                  'url' => '',
                  'flags' => '0',
                  'value' => '0',


                },


       324234 => {
                  'hosts' => [
                               {
                                 'name' => 'pc45657ba34gy0423',
                                 'maintenance_type' => '0',
                                 'hostid' => '19439'
                               }
                             ],
                  'priority' => '1',
                  'status' => '0',
                  'dependencies' => [],
                  'templateid' => '9896452',
                  'comments' => '',
                  'state' => '0',

                  'triggerid' => '10324234',
                  'expression' => '{14167689}=0',
                  'error' => '',
                  'url' => '',
                  'flags' => '0',
                  'value' => '0',
                  'value_flags' => '0',
                  'lastchange' => '1420266068',
                  'type' => '0'

}
};

为了便于阅读,代码与数据断开连接:

foreach my $k (keys %$triggers1) {
    print "key=$k";
    foreach my $h (@{$triggers1->{$k}{hosts}}) {
        print "\nname=",$h->{name} // 'not defined';
        //                use this ^^ to avoid Use of uninitialized value in print at 
    }
    print "\nvalue=",$triggers1->{$k}{value} // 'not defined',"\n";
}

输出:

key=324234
name=pc45657ba34gy0423
value=0
key=10305122
name=pc4b12cf254444
value=0