Accessing Specific OTRS Dynamic Field value via SOAP - perl

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

Related

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

perl - How to loop access hashref?

I have some data in hashref format. I fetch data from graph.facebook.com
How to loop access?
$var = \{
'data' => [
{
'id' => '312351465029_10154168935475030',
'name' => 'Timeline Photos 1'
},
{
'name' => 'Bangchak\'s cover photo',
'id' => '312351465029_10154168087455030',
},
{
'id' => '312351465029_10154168081875030',
'name' => 'Timeline Photos 2',
}
],
'paging' => {
'previous' => 'https://graph.facebook.com/v2.6/312351465029/2',
'next' => 'https://graph.facebook.com/v2.6/312351465029/3'
}
};
These code Didn't work.
foreach $m ($var->{data})
{
if ( $m->{name} =~ /Timeline/i )
{
print "id = $m->{id}\n";
}
}
You need to dereference the array (perldoc perldsc):
use warnings;
use strict;
my $var = {
'data' => [
{
'id' => '312351465029_10154168935475030',
'name' => 'Timeline Photos 1'
},
{
'name' => 'Bangchak\'s cover photo',
'id' => '312351465029_10154168087455030',
},
{
'id' => '312351465029_10154168081875030',
'name' => 'Timeline Photos 2',
}
],
'paging' => {
'previous' => 'https://graph.facebook.com/v2.6/312351465029/2',
'next' => 'https://graph.facebook.com/v2.6/312351465029/3'
}
};
foreach my $m (#{ $var->{data} }) {
if ( $m->{name} =~ /Timeline/i )
{
print "id = $m->{id}\n";
}
}
__END__
id = 312351465029_10154168935475030
id = 312351465029_10154168081875030

Extracting specific elements from hash

my %hash = {
'student1' => {
'Name' => 'aaa',
'Age' => '20',
'Subjects' => ['Maths','Science']
},
'student2' => {
'Name' => 'bbb',
'Age' => '22',
'Subjects' => ['English','Science']
}
}
my $hashRef = \%hash;
how do i extract the second subject name from this using hashref ?
Your declaration of %hash is incorrect, do this instead:
my %hash = (
'student1' => {
'Name' => 'aaa',
'Age' => '20',
'Subjects' => ['Maths','Science']
},
'student2' => {
'Name' => 'bbb',
'Age' => '22',
'Subjects' => ['English','Science']
}
);
Note the parens instead og brace.
Then to get the second subject :
say $hashRef->{student1}{Subjects}[1];
Your code is wrong, { } creates a hashref and you are storing it in a hash. You should do:
my %hash = (
'student1' => {
'Name' => 'aaa',
'Age' => '20',
'Subjects' => ['Maths','Science']
},
'student2' => {
'Name' => 'bbb',
'Age' => '22',
'Subjects' => ['English','Science']
}
);
my $hashRef = \%hash;
or even better:
my $hashref = {
student1 => { ... },
student2 => { ... },
};
Then you can access with:
$hashRef->{student2}->{Subjects}[1]
Subjects are an array reference inside a hash inside a hash.
$hashRef->{student1}{Subjects}[1]
Also, do not use curly brackets to initilize a hash, they create an anonymous hash. Use round parentheses:
my %hash = ( ... );

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

Create, populate and access a multi-dimensional array in Perl

I have the data structure (given below), can you please let me know how to populate and also to access the elements in the simplest form anytime I want. Which is the best way to implement, like Hash of Hashes or Array of Arrays or anything else ? There won't be too many elements in the array, the only problem is the its a multi-level kind of array
%Array = (
'Item0' => {
'Name' => 'Item0_Name',
'Attribs' => {
'Attr1' => {
'Name' => 'Attr1_Name',
'Num' => 'Attr1_Num'
},
'Attr2' => {
'Name' => 'Attr2_Name',
'Num' => 'Attr2_Num'
}
}
},
'Item1' => {
'Name' => 'Item1_Name',
'Attribs' => {
'Attr1' => {
'Name' => 'Attr1_Name',
'Num' => 'Attr1_Num'
},
'Attr2' => {
'Name' => 'Attr2_Name',
'Num' => 'Attr2_Num'
}
}
}
);
The official resource for this is the Perl Data Structure Cookbook (perldoc perldsc).
For the data shown, it seems that an array of hashrefs is sufficient since the keys shown in the OP are simply a stringified index. The same comment applies for 'Attribs':
my #array = (
{
'Name' => 'Item0_Name',
'Attribs' => [
{
'Name' => 'Attr1_Name',
'Num' => 'Attr1_Num'
},
{
'Name' => 'Attr2_Name',
'Num' => 'Attr2_Num'
}
],
},
{
'Name' => 'Item1_Name',
'Attribs' => [
{
'Name' => 'Attr1_Name',
'Num' => 'Attr1_Num'
},
{
'Name' => 'Attr2_Name',
'Num' => 'Attr2_Num'
}
],
},
);