阵列哈希阵列的哈希

时间:2016-09-08 20:20:46

标签: perl perl-data-structures

我在perl中使用hash的以下输出:

$VAR1 = {
     'ins_api' => {
          'sid' => 'eoc',
          'outputs' => {
              'output' => [
                   {
                   'body' => {
                       'TABLE_interface' => {
                            'ROW_interface' => [
                                 {
                                 'vdc_lvl_in_pkts' => 17081772,
                                 'vdc_lvl_in_avg_bits' => 3128,
                                 'eth_autoneg' => 'on',
                                 'eth_speed' => '1000 Mb/s',
                                 'admin_state' => 'up',
                                 'vdc_lvl_out_mcast' => '65247',
                                 'state' => 'up',
                                 'eth_mtu' => '1500',
                                 'eth_hw_addr' => '78ba.f9ad.b248',
                                 'eth_mdix' => 'off',
                                 'interface' => 'mgmt0',
                                 'eth_ip_addr' => '10.56.32.84',
                                 'eth_bw' => 1000000,
                                 'vdc_lvl_in_avg_pkts' => '3',
                                 'vdc_lvl_out_bytes' => '3463952330',
                                 'vdc_lvl_in_ucast' => '7653891',
                                 'eth_ip_prefix' => '10.',
                                 'eth_rxload' => '1',
                                 'eth_txload' => '1',
                                 'eth_reliability' => '255',
                                 'eth_dly' => 10,
                                 'vdc_lvl_in_mcast' => '8742911',
                                 'eth_ip_mask' => 24,
                                 'eth_bia_addr' => '78ba.f9ad.b248',
                                 'eth_duplex' => 'full',
                                 'vdc_lvl_out_pkts' => '8668507',
                                 'vdc_lvl_out_avg_pkts' => '1',
                                 'vdc_lvl_in_bcast' => '684970',
                                 'vdc_lvl_out_avg_bits' => '1840',
                                 'medium' => 'broadcast',
                                 'vdc_lvl_out_bcast' => '5',
                                 'vdc_lvl_out_ucast' => '8603255',
                                 'eth_ethertype' => '0x0000',
                                 'vdc_lvl_in_bytes' => '1985125644',
                                 'eth_hw_desc' => 'GigabitEthernet'
                                },
                                {
                                 'eth_babbles' => '0',
                                 'eth_outbytes' => '7362149107971',
                                 'eth_outucast' => '16348249961',
                                 'eth_clear_counters' => 'never',
                                 'eth_watchdog' => '0',
                                 'eth_inpkts' => 8644872191,
                                 'eth_inbytes' => '3415386845315',
                                 'eth_out_flowctrl' => 'off',
                                 'eth_bad_proto' => '0',
                                 'eth_frame' => '0',
                                 ----- output omitted -------
                                },

通过ROW_interface数组循环并打印一些元素的最佳方法是什么?我只是想在ROW_interface数组中获取元素。

2 个答案:

答案 0 :(得分:0)

my $ROW_Interfaces = $output->{body}{TABLE_interface}{ROW_interface};

for my $ROW_Interfaces (@$ROW_Interfaces) {
   ...
}

似乎可以有多个output,因此您必须同样找到合适的一个。

答案 1 :(得分:0)

与@ ikegami的答案类似,但处理多个output条目。 if defined...存在,因为结构不完整,我不确定每个条目是否具有相同的密钥。

for my $output (@{ $VAR1->{ins_api}{outputs}{output} }){
    for my $row_int (@{ $output->{body}{TABLE_interface}{ROW_interface} }){
        print "$row_int->{eth_frame}\n" if exists $row_int->{eth_frame};
    }
}