Extracting data from a multi-level hash - perl

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?

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

Related

Disabling the past dates

I want to disable the past dates from the laravel-backpack component date-range-picker.
`$this->crud->addField([
'name' => ['event_start_date', 'event_end_date'],
'type' => 'date_range',
'label' => "Start Date",
'wrapperAttributes'=>['class'=>'form-group col-md-6'],
'default' => [date("Y-m-d"), date("Y-m-d")],
'date_range_options' => [
'todayBtn' => 'linked',
'timePicker' => true,
'format' => 'YYYY-MM-DD'
],
'allows_null' => true,
]);`
How can I do that?
$this->crud->addField([
'name' => ['event_start_date', 'event_end_date'],
'type' => 'date_range',
'label' => "Start Date",
'wrapperAttributes'=>['class'=>'form-group col-md-6'],
//'default' => [date("Y-m-d"), date("Y-m-d")],
'date_range_options' => [
'todayBtn' => 'linked',
// options sent to daterangepicker.js
//'timePicker' => true,
'startDate' => date("Y-m-d"),
'endDate' => date("Y-m-d"),
'minDate'=> date("Y-m-d"),
'locale' => ['format' => 'YYYY-MM-DD']
],
'allows_null' => true,
]);

Perl dynamic hash of hash errors

I am looping over a query and returning results. I am trying to add a hash to another hashes. But running into trouble.
my %users_data;
while($sth->fetch)
{
$mygroup =>
{
'fname' => $fname,
'lname' => $lname,
'address' =>
{
'street' => $street,
'city' => $city,
},
'id' => $uid,
},
}
how do I add the $mygroup hash to %users_data hash?
long hand would be.
my %users_data = (
'salesmanager' =>
{
'fname' => 'mike',
'lname' => 'john',
'address' =>
{
'street' => '123 street',
'city' => 'Brooklyn',
},
'id' => 12,
},
'garagemanager' =>
{
'fname' => 'Mark',
'lname' => 'Jones',
'address' =>
{
'street' => '355 street',
'city' => 'Brooklyn',
},
'id' => 13,
},
)
Simply access the target key and assign its new value:
my %users_data;
while($sth->fetch)
{
$users_data{$mygroup} = {
'fname' => $fname,
'lname' => $lname,
'address' =>
{
'street' => $street,
'city' => $city,
},
'id' => $uid,
};
}

Accessing Specific OTRS Dynamic Field value via SOAP

How do I further access this dynamic field value? Upon using below dumper,
print Dumper( $Body->{$ResponseKey} );
The result is :
$VAR1 = {
'Ticket' => {
'Title' => 'TPLUS Service PIC',
'DynamicField' => [
{
'Value' => '43312',
'Name' => 'BugID'
},
{
'Value' => '6',
'Name' => 'OTRSMV'
},
{
'Value' => '6.13',
'Name' => 'OTRSPLV'
},
{
'Value' => 'Dev',
'Name' => 'OTRSUse'
},
{
'Value' => '2018-03-02 00:28:00',
'Name' => 'RefDate'
},
{
'Value' => '0',
'Name' => 'RefNumber'
},
{
'Value' => '',
'Name' => 'StartTime'
}
],
'StateType' => 'open',
'SLAID' => ''
}
};
How can I access the single value of DynamicField->RefDate ? Thanks
my $fields = $Body->{$ResponseKey}{Ticket}{DynamicField};
my ($ref_date) =
map $_->{Value},
grep $_->{Name} eq 'RefDate',
#$fields;
or
my %fields;
$fields{ $_->{Name} } = $fields{ $_->{Value} }
for #{ $Body->{$ResponseKey}{Ticket}{DynamicField} };
my $ref_date = $fields{RefDate};

cakephp3.1: Cannot limit the fields loaded in association

I execute the following query:
$sites = $this->Sites->find('all')
->select(['id', 'name'])
->contain([
'Users' => function ($q) {
return $q->select(['id', 'owner_id', 'firstname', 'lastname']);
},
])
->matching('Users', function ($q) use($owner_id)
{
return $q->where([
'Users.owner_id' => $owner_id
]);
})
->all();
So I insert a select() statement in Users association to restrict the retrieved fields to id, owner_id, firstname and lastname but it returns the following result:
'items' => [
(int) 0 => object(App\Model\Entity\Site) {
'id' => (int) 7,
'name' => 'the site',
'orders' => [],
'users' => [
(int) 0 => object(App\Model\Entity\User) {
'id' => (int) 49,
'owner_id' => (int) 42,
'firstname' => 'Ed',
'lastname' => 'Gavin',
'username' => 'EdG',
'password' => '$2y$10$jHN7/mLwkh7O.aOHVmAeK.PnY9IrT/tTpcKlH25Il0yLtveE6.Onu',
'email' => 'ed.gavin#yopmail.com',
'phone' => '',
'role' => 'owner',
'active' => (int) 1,
'token' => '8cf196e6b45e972fbfec11eb7b80748d',
'origin' => '',
'_joinData' => object(App\Model\Entity\SitesUser) {
Can you tell me why the select() is not respected for Users?

Perl printing second level hash keys in a nested hash

How do I print all my second level hash keys (sig_qtr, date, range, etc.) given a hash like such:
my $xml = XMLin("./${spec_file}", ForceArray => ['range', 'constant', 'question', 'date', 'sig_yr', 'sig_qtr', 'sig_mth'], KeyAttr => {});
print Dumper $xml->{entities};
print dumper output of hash:
$VAR1 = {
'sig_qtr' => [
{
'name' => 'q1',
'label' => 'q1'
},
{
'name' => 'q4',
'label' => 'q4'
}
],
'date' => [
{
'name' => 'y2_mth',
'label' => 'pryr_mth_curr'
},
{
'name' => 'y3_pod6_qtr4',
'label' => 'curr_qtd4'
}
],
'range' => [
{
'name' => 'y0_jun',
'end' => '20100631',
'start' => '20100601'
},
{
'name' => 'y3_oct',
'end' => '20131031',
'start' => '20131001'
}
],
'constant' => [
{
'spec' => '99999999 and 99999999',
'name' => 'none_sixmth'
}
],
'sig_yr' => [
{
'name' => 'y1_sig',
'label' => 'ye11'
},
{
'name' => 'y3_sig',
'label' => 'ytd'
}
],
'sig_mth' => [
{
'name' => 'y3_nov',
'label' => 'nov12'
},
{
'name' => 'y3_oct',
'label' => 'oct13'
}
],
'question' => [
{
'name' => 'ltrq',
'label' => 'q9'
},
{
'name' => 'nextprod',
'label' => 'q12a'
}
],
'backfill' => {
'label' => 'bf_period'
},
'year' => {
'current' => '2013'
}
};
would be even better if keys are put into an array.
Thanks.
print "$_\n" for keys %{ $xml->entities };
To put them into an array,
my #keys = keys %{ $xml->entities };