perl - Array of hashes manipulation -
i'm running script below query monitoring system , results in array of hashes. best way access individual hashes , elements?
use strict; use warnings; use yms::client::monstatus; use getopt::long; use dbi::dumper; use data::dumper; use yms::client::monstatus::filter; $username = "username"; $password = "passwd"; $cluster = "aso"; #my $filter = yms::client::monstatus::filter(); $client = yms::client::monstatus->new($cluster); $client->verbose("on"); $client->byauth({username => $username, password => "$password"}); $client->yms_rotation("bot.ops.com:4080"); $client->debug("on"); $filter = yms::client::monstatus::filter->new(); $filter->add("cluster", "=", "aso"); $filter->add("host", "in", "<hostname>"); #$filter->add("service", "=", "prod-deploy"); $filter->add("status", "=", "ok"); %args = ( "filters" => $filter, "times" => { "start" => "now - 4 hours", "end" => "now" }, ); $client->filters($filter); #$client->saved_list(); $client->status_list(\%args); @result = $client->output; $field = scalar (@result); #print dumper (@result); #my $ref = $result[0][0]->[0]; #print dumper ($result[0][0]); print $field; ( $i = 0; $i < $field; $i++ ) { print $result[$i]{'notification_enabled'} ."\n"; }
the output querying api below.
$var1 = [ { 'acknowledged' => 'n/a', 'check_type' => 'active', 'current_attempt' => 1, 'deleted' => 0, 'dimensions' => { 'cluster' => '<cluster>', 'service' => '<servicename>', 'host' => 'hostfoobaar' }, 'flapping' => 'n/a', 'frequency' => 0, 'hidden' => 0, 'last_ack_time' => 0, 'last_check_time' => 1432875229, 'last_comment_time' => 0, 'last_delete_time' => 1432875229, 'last_downtime_time' => 0, 'last_notif_time' => 0, 'last_state_change' => 1432875094, 'last_status_change' => 1432875094, 'max_attempt' => 1, 'msg_length' => 10, 'notification_enabled' => 'enabled', 'num_comments' => 0, 'quality_metric' => 0, 's_0' => 0, 's_1' => 0, 's_2' => 0, 's_3' => 0, 's_4' => 0, 's_5' => 0, 's_6' => 0, 's_7' => 0, 's_8' => 0, 's_9' => 0, 'scheduled_downtime' => 'no', 'source' => '', 'state' => 'hard', 'status' => 'ok', 'status_msg' => 'status ok', 'trap' => 1, 'ts_0' => 1432875229, 'ts_1' => 1432875094, 'ts_2' => 0, 'ts_3' => 0, 'ts_4' => 0, 'ts_5' => 0, 'ts_6' => 0, 'ts_7' => 0, 'ts_8' => 0, 'ts_9' => 0 } ];`
here's output of print dumper \@results
$var1 = [ [ { 'acknowledged' => 'n/a', 'check_type' => 'active', 'current_attempt' => 1, 'deleted' => 0, 'dimensions' => { 'cluster' => 'aso', 'service' => 'system_launch', 'host' => 'hostfoobar' }, 'flapping' => 'n/a', 'frequency' => 0, 'hidden' => 0, 'last_ack_time' => 0, 'last_check_time' => 1432875317, 'last_comment_time' => 0, 'last_delete_time' => 0, 'last_downtime_time' => 0, 'last_notif_time' => 0, 'last_state_change' => 1432875317, 'last_status_change' => 1432875317, 'max_attempt' => 1, 'msg_length' => 10, 'notification_enabled' => 'enabled', 'num_comments' => 0, 'quality_metric' => 0, 's_0' => 0, 's_1' => 0, 's_2' => 0, 's_3' => 0, 's_4' => 0, 's_5' => 0, 's_6' => 0, 's_7' => 0, 's_8' => 0, 's_9' => 0, 'scheduled_downtime' => 'no', 'source' => '', 'state' => 'hard', 'status' => 'ok', 'status_msg' => 'status ok', 'trap' => 1, 'ts_0' => 1432875317, 'ts_1' => 0, 'ts_2' => 0, 'ts_3' => 0, 'ts_4' => 0, 'ts_5' => 0, 'ts_6' => 0, 'ts_7' => 0, 'ts_8' => 0, 'ts_9' => 0 }, { 'acknowledged' => 'n/a', 'check_type' => 'active', 'current_attempt' => 1, 'deleted' => 0, 'dimensions' => { 'cluster' => 'cluster', 'service' => 'prod-deploy', 'host' => 'hostfoobar' }, 'flapping' => 'n/a', 'frequency' => 0, 'hidden' => 0, 'last_ack_time' => 0, 'last_check_time' => 1432875229, 'last_comment_time' => 0, 'last_delete_time' => 1432875229, 'last_downtime_time' => 0, 'last_notif_time' => 0, 'last_state_change' => 1432875094, 'last_status_change' => 1432875094, 'max_attempt' => 1, 'msg_length' => 10, 'notification_enabled' => 'enabled', 'num_comments' => 0, 'quality_metric' => 0, 's_0' => 0, 's_1' => 0, 's_2' => 0, 's_3' => 0, 's_4' => 0, 's_5' => 0, 's_6' => 0, 's_7' => 0, 's_8' => 0, 's_9' => 0, 'scheduled_downtime' => 'no', 'source' => '', 'state' => 'hard', 'status' => 'ok', 'status_msg' => 'status ok', 'trap' => 1, 'ts_0' => 1432875229, 'ts_1' => 1432875094, 'ts_2' => 0, 'ts_3' => 0, 'ts_4' => 0, 'ts_5' => 0, 'ts_6' => 0, 'ts_7' => 0, 'ts_8' => 0, 'ts_9' => 0 }, { 'acknowledged' => 'n/a', 'check_type' => 'active', 'current_attempt' => 1, 'deleted' => 0, 'dimensions' => { 'cluster' => 'cluster', 'query' => 'foobar', 'service' => 'foobar', 'host' => 'hostfoobar' }, 'flapping' => 'n/a', 'frequency' => 60, 'hidden' => 0, 'last_ack_time' => 1433949747, 'last_check_time' => 1433949867, 'last_comment_time' => 1421368592, 'last_delete_time' => 1433949867, 'last_downtime_time' => 1421376467, 'last_notif_time' => 1427825899, 'last_state_change' => 1407310478, 'last_status_change' => 1433949867, 'max_attempt' => 1, 'msg_length' => 172, 'notification_enabled' => 'enabled', 'num_comments' => 8, 'quality_metric' => 0, 's_0' => 0, 's_1' => 1, 's_2' => 0, 's_3' => 0, 's_4' => 0, 's_5' => 0, 's_6' => 0, 's_7' => 0, 's_8' => 0, 's_9' => 0, 'scheduled_downtime' => 'no', 'source' => '', 'state' => 'hard', 'status' => 'ok', 'status_msg' => 'unknown.', 'trap' => 0, 'ts_0' => 1433949867, 'ts_1' => 1433949747, 'ts_2' => 1433949687, 'ts_3' => 1433949627, 'ts_4' => 1433949567, 'ts_5' => 1433949507, 'ts_6' => 1433949447, 'ts_7' => 1433949387, 'ts_8' => 1433949327, 'ts_9' => 1433949267 }, { 'acknowledged' => 'n/a', 'check_type' => 'active', 'current_attempt' => 1, 'deleted' => 0, 'dimensions' => { 'cluster' => 'cluster', 'service' => 'production_deployment', 'host' => 'hostfoobar' }, 'flapping' => 'n/a', 'frequency' => 0, 'hidden' => 0, 'last_ack_time' => 0, 'last_check_time' => 1432875270, 'last_comment_time' => 0, 'last_delete_time' => 0, 'last_downtime_time' => 0, 'last_notif_time' => 0, 'last_state_change' => 1432875270, 'last_status_change' => 1432875270, 'max_attempt' => 1, 'msg_length' => 10, 'notification_enabled' => 'enabled', 'num_comments' => 0, 'quality_metric' => 0, 's_0' => 0, 's_1' => 0, 's_2' => 0, 's_3' => 0, 's_4' => 0, 's_5' => 0, 's_6' => 0, 's_7' => 0, 's_8' => 0, 's_9' => 0, 'scheduled_downtime' => 'no', 'source' => '', 'state' => 'hard', 'status' => 'ok', 'status_msg' => 'status ok', 'trap' => 1, 'ts_0' => 1432875270, 'ts_1' => 0, 'ts_2' => 0, 'ts_3' => 0, 'ts_4' => 0, 'ts_5' => 0, 'ts_6' => 0, 'ts_7' => 0, 'ts_8' => 0, 'ts_9' => 0 } ] ];
below script shows how iterate on loop perl idiomatic way. doesn't go deeper 3rd level, should enough give ideas. in while() loop, shows how dereference inner hash whole , without needing incrementor, , shows how dereference , access individual items within structure.
edit: we've found op has array of arrays of hashes. code updated.
#!/usr/bin/perl use warnings; use strict; @aoh = ( [ { 1 => 'a', 2 => 'b', 3 => 'c', }, { 'z' => 26, 'd' => { 100 => '3rd level', }, 'y' => 25, 'x' => 24, }, ], ); $element (@aoh){ $level_2_element (@{ $element }){ while (my ($key, $value) = (each %{ $level_2_element })){ if (ref($value) eq 'hash'){ while (my ($k, $v) = (each %{$value})){ print "$key: -- $k: $v\n"; } } else { print "$key: $value\n"; } } } } print "element 0 0, key 1: $aoh[0]->[0]{1}\n"; print "element 0 1, key x: $aoh[0]->[1]{x}\n";
Comments
Post a Comment