Search code examples
perlhashzabbix

Extracting data from a multi-level hash


I am getting the data structure below as a response from a web service call.

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

Output

          $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'

    };

etc

There are multiple similar records

From this output, I want to print the values of 'name' and 'value'.

How do I print this using Perl?


Solution

  • How about:

        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'
    
    }
    };
    

    Code disconnected from data for readability:

    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";
    }
    

    Output:

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