从多级哈希中提取数据

时间:2015-01-06 15:02:53

标签: perl hash zabbix

我将下面的数据结构作为来自Web服务调用的响应。

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打印?

1 个答案:

答案 0 :(得分:0)

怎么样:

    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