dereferencing data from multiple hash and array data structure in perl - perl

Trying to extract data from detailed -> doc -> params -> parameters -> name in following DoxyDocs1.pm file; also including sample of script that extracts some data, but having trouble extracting params -> parameters -> name. Uncomment commented sections to see data.
#!/bin/perl
use Data::Dumper;
use warnings;
require "DoxyDocs1.pm";
print "API Content Analyzer\n";
&GenerateClassInfo($doxydocs->{classes});
sub GenerateClassInfo()
{
my ($classes) = #_;
foreach my $class (#$classes) {
print "\nClass name is: $class->{name}\n";
foreach my $pubmeth (#{$class->{public_methods}->{members}}) {
print "\n" if exists $pubmeth->{name};
print "\tpublic methods include: $pubmeth->{name}\n";
my ($key, $pmcontent) = each (#{$pubmeth->{detailed}->{doc}});
print "\t\tdescription: $pmcontent->{content}\n" if exists $pmcontent->{content};
# foreach my $pmp (#{$pubmeth->{detailed}->{doc}}) {
# print "\t\t";
# print Dumper($pmp);
# }
print "\t\tkind: $pubmeth->{kind}\n";
print "\t\ttype: $pubmeth->{type}\n" if exists $pubmeth->{type};
}
foreach my $privmeth (#{$class->{private_methods}->{members}}) {
print "\n" if exists $privmeth->{name};
print "\tprivate methods include: $privmeth->{name}\n";
my ($key, $pmcontent) = each (#{$privmeth->{detailed}->{doc}});
print "\t\tdescription: $pmcontent->{content}\n" if exists $pmcontent->{content};
# foreach my $info (#{$privmeth->{detailed}->{doc}}) {
# print "\t\t";
# print Dumper($info);
# }
print "\t\tkind: $privmeth->{kind}\n";
print "\t\ttype: $privmeth->{type}\n" if exists $privmeth->{type};
}
}
}
Example DoxyDocs1.pm file
$doxydocs=
{
classes => [
{
name => 'Panoply::Composite',
public_methods => {
members => [
{
kind => 'function',
name => 'addChild',
virtualness => 'non_virtual',
protection => 'public',
static => 'no',
brief => {},
detailed => {
doc => [
{
type => 'text',
content => 'Add a child to the container '
},
params => [
{
parameters => [
{
name => 'child'
}
],
doc => [
{
type => 'text',
content => 'is the child element to add'
}
]
}
]
]
},
type => 'void',
const => 'no',
volatile => 'no',
parameters => [
{
declaration_name => 'child',
type => 'Ptr'
}
]
},
{
kind => 'function',
name => 'operator<',
virtualness => 'non_virtual',
protection => 'public',
static => 'no',
brief => {},
detailed => {
doc => [
{
type => 'text',
content => 'Less than operator'
},
{
type => 'parbreak'
},
params => [
{
parameters => [
{
name => 'rval'
}
],
doc => [
{
type => 'text',
content => 'The '
},
{
type => 'url',
link => 'classPanoply_1_1Package',
content => 'Package'
},
{
type => 'text',
content => ' against which we are comparing this one. '
}
]
}
],
{
return => [
{
type => 'text',
content => 'true if this.packageID < rval.packageID, false otherwise.'
}
]
}
]
},
type => 'bool',
const => 'yes',
volatile => 'no',
parameters => [
{
declaration_name => 'rval',
type => 'const Composite &'
}
]
},
]
},
private_methods => {
members => [
{
kind => 'function',
name => 'addChild',
virtualness => 'virtual',
protection => 'private',
static => 'no',
brief => {},
detailed => {
doc => [
{
type => 'text',
content => 'Add a child to the container '
},
params => [
{
parameters => [
{
name => 'child'
}
],
doc => [
{
type => 'text',
content => 'is the child element to add '
}
]
},
{
parameters => [
{
name => 'parent'
}
],
doc => [
{
type => 'parbreak'
},
{
type => 'text',
content => 'is this own parent, except in weak pointer format to avoid a memory leak'
}
]
}
]
]
},
type => 'virtual void',
const => 'no',
volatile => 'no',
parameters => [
{
declaration_name => 'child',
type => 'Ptr'
},
{
declaration_name => 'parent',
type => 'Ptr'
}
]
},
]
},
}
]
};
1;

You said you wanted
detailed -> doc -> params -> parameters -> name
but that's missing many indexes. Which doc, param and parameters do you want?
detailed -> doc -> ??? -> params -> ??? -> parameters -> ??? -> name
The syntax is:
$member->{detailed}->{doc}->[$i]->{params}->[$j]->{parameters}->[$k]->{name}
Or for short:
$member->{detailed}{doc}[$i]{params}[$j]{parameters}[$k]{name}
If you want to loop over every doc, params, parameter, you can use:
my $docs = $member->{detailed}{doc};
for my $doc (#$docs) {
my $params = $doc->{params};
for my $param (#$params) {
my $parameters = $param->{parameters};
for my $parameter (#$parameters) {
...
}
}
}
(Why is doc singular and params and parameters plural??? Why do params have parameters???)

Related

Looping through hashes in Perl gives odd result - example script provided

I'm revisiting a perl application that I built several years ago. I have to rebuild some of it. But today I'm stuck. I'm having some trouble with hashes. I have this test script that loops through some hashes. What I don't understand is that the second time the last loop gives 'pid1' => $VAR1->[1]{'deal'}{'pid1'} as output. I'm expecting a hash with product data. What am I doing wrong?
#!usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my %stores;
push #{$stores{'store1'}}, 'pid1';
push #{$stores{'store1'}}, 'pid2';
push #{$stores{'store2'}}, 'pid1';
print Dumper(\%stores);
my %products = (
'pid1' => {
'name' => 'Product 1',
'color' => 'red'
},
'pid2' => {
'name' => 'Product 2',
'color' => 'blue'
}
);
print Dumper \%products;
my #offers;
foreach my $storeid (keys %stores) {
foreach my $pid (#{$stores{$storeid}}) {
my %offer;
$offer{$storeid}{'deal'}{$pid} = $products{$pid};
push(#offers, %offer);
}
}
print Dumper(\#offers);
$VAR1 = {
'store1' => [
'pid1',
'pid2'
],
'store2' => [
'pid1'
]
};
$VAR1 = {
'pid2' => {
'name' => 'Product 2',
'color' => 'blue'
},
'pid1' => {
'color' => 'red',
'name' => 'Product 1'
}
};
$VAR1 = [
'store1',
{
'deal' => {
'pid1' => {
'color' => 'red',
'name' => 'Product 1'
}
}
},
'store1',
{
'deal' => {
'pid2' => {
'name' => 'Product 2',
'color' => 'blue'
}
}
},
'store2',
{
'deal' => {
'pid1' => $VAR1->[1]{'deal'}{'pid1'}
}
}
];
It means
$VAR1->[1]{'deal'}{'pid1'} # $offers[1]{'deal'}{'pid1'}
and
$VAR1->[5]{'deal'}{'pid1'} # $offers[5]{'deal'}{'pid1'}
are both references to the same hash, which looks like
{
'color' => 'red',
'name' => 'Product 1'
}
Maybe it's clearer if you use local $Data::Dumper::Purity = 1; to produce code that can actually be executed.
$VAR1 = [
'store1',
{
'deal' => {
'pid1' => {
'color' => 'red',
'name' => 'Product 1'
}
}
},
'store1',
{
'deal' => {
'pid2' => {
'name' => 'Product 2',
'color' => 'blue'
}
}
},
'store2',
{
'deal' => {
'pid1' => {}
}
}
];
$VAR1->[5]{'deal'}{'pid1'} = $VAR1->[1]{'deal'}{'pid1'};

Perl hash add values

I am trying to push values into hash. I want to add the values under 'par3'.
e.g.
$VAR1 = { 'obj1' => ['par1',
'par2',
'par3' => ['par4','par5','par6',....]]}
I should also be able to add elements into 'par3' in case 'obj1'-'par1'-'par2'-'par3' matches.
So far I have this, but I can't figure out how can I add "the second level" under 'par3':
push #{$a{$obj}},$par1,$par2,$par3
[ ... ] is a reference to an array. Array elements are scalars. So it is not possible to directly have the structure you seem to be requesting (ie. the par3 => [ ... ] pseudocode from your question). See perldsc
It's not obvious what you are trying to do but a couple of possible ideas might be to use a reference to a hash, or to replace the array with a hash:
use Data::Dumper;
$Var2a = {
'obj1' => [
'par1',
'par2',
{ 'par3' => undef, }
],
};
push #{ $Var2a->{obj1}[2]{par3} }, 'par4', 'par5', 'par6';
print Dumper $Var2a;
$Var2b = {
'obj1' => {
'par1' => undef,
'par2' => undef,
'par3' => undef,
},
};
push #{ $Var2b->{obj1}{par3} }, 'par4', 'par5', 'par6';
print Dumper $Var2b;
I think you want to add children to par3. As such, I think you want pars to have children. If so, you need a different data structure.
# Ordered
my $obj1 = [
{ name => 'par1', children => [ ] },
{ name => 'par2', children => [ ] },
{ name => 'par3', children => [
{ name => 'par4', children => [ ] },
{ name => 'par5', children => [ ] },
{ name => 'par6', children => [ ] },
] },
];
or
# Unordered
my $obj1 = {
par1 => { },
par2 => { },
par3 => {
par4 => { },
par5 => { },
par6 => { },
},
};
To append par7 to par3's children, you would use
# Ordered
use List::Util qw( first );
my $par7 = { name => 'par7', children => [ ] };
my $par3 = first { $_->{ name } eq 'par3' } #$obj1
or die( "par3 not found" );
push #{ $par3->{ children } }, $par7;
or
# Unordered
$obj1->{ par3 }{ par7 } = { };

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

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

Convert a flat datastructure into a tree

I have an array of hashes. Each element in the array is a node in a hierarchical tree and has referential data for who the parent is. I will have thousands and hundreds of thousands of nodes in the tree... essentially an unknown set of nodes has to be converted to JSON (shown below) for use with http://bl.ocks.org/robschmuecker/7880033
UPDATE: position_id is a node in the heretical tree. placement_id is the parent's position_id (adjacency referential tree).
UPDATE: Here's the full AoH Data::Dumper result with Nested Set and Adjacency result from a modified version of DBIx::Tree::NestedSet (custom).
$VAR1 = [
{
'lft' => '673',
'id' => '109',
'date_created' => '2015-08-15',
'level' => '7',
'user_id' => '13',
'placement_id' => '11',
'position_id' => '13',
'status' => '1',
'structure_id' => '1',
'rght' => '684'
},
{
'placement_id' => '13',
'position_id' => '22',
'status' => '1',
'structure_id' => '1',
'rght' => '679',
'lft' => '674',
'date_created' => '2015-08-15',
'id' => '116',
'level' => '8',
'user_id' => '22'
},
{
'user_id' => '101',
'level' => '9',
'id' => '200',
'date_created' => '2015-08-15',
'lft' => '675',
'rght' => '676',
'structure_id' => '1',
'status' => '1',
'position_id' => '101',
'placement_id' => '22'
},
{
'date_created' => '2015-08-15',
'id' => '201',
'level' => '9',
'user_id' => '374',
'lft' => '677',
'structure_id' => '1',
'rght' => '678',
'placement_id' => '22',
'position_id' => '374',
'status' => '1'
},
{
'lft' => '680',
'user_id' => '95',
'level' => '8',
'id' => '117',
'date_created' => '2015-08-15',
'status' => '1',
'position_id' => '95',
'placement_id' => '13',
'rght' => '681',
'structure_id' => '1'
}
];
THIS IS THE GOAL, For this example I need to end up with:
{
"name": "13",
"children": [
{
"name": "22",
"children": [
{
"name": "101"
},
{
"name": "374"
}
]
},
{
"name": "95"
}
]
}
You can also see the format I am trying to arrive at here (minus size):
http://bl.ocks.org/robschmuecker/7880033#flare.json
My failed approach(es) included various attempts at looping through the array of hashes to create a recursive Hash of Hashes that can then be used with the JSON Perl module to create the actual JSON I need.
my $data = [
{ position_id => 123, placement_id => undef },
{ position_id => 456, placement_id => 123 },
{ position_id => 789, placement_id => 123 },
# ...
];
my $roots;
{
my %recs_by_name;
my %children_by_parent_name;
for my $row (#$data) {
my $name = $row->{position_id};
my $parent_name = $row->{placement_id};
my $rec = {
name => $name,
};
push #{ $children_by_parent_name{$parent_name // 'root'} }, $rec;
$recs_by_name{$name} = $rec;
}
$roots = delete($children_by_parent_name{root}) || [];
for my $name (keys(%children_by_parent_name)) {
my $children = $children_by_parent_name{$name};
if ( my $rec = $recs_by_name{$name} ) {
$rec->{children} = $children;
} else {
die("Parent $name doesn't exist.\n");
push #$roots, #$children;
}
}
}
print(Dumper($roots));
Tested.
You appear to have the depth of each node available to you (level). Simpler code could be used if your data was sorted by increasing depths.
While it was #ikegami who ultimately answered the question that led to the solution. I believe the following adaptation adds 4 important elements/clarifications I found helpful, and thought others reading this question and answer would also find useful.
1- Clear addition of all key,value pairs from the originating AoH to the resulting HOH. See while loop.
2- A Child node counter.
3- Inclusion and use of the encode_json function from JSON
4- The result is also an Array with a Hash as the first element. Newbies (like me) might find the explicit #{$roots}[0] passed to encode_json as helpful.
At first I had a similar adapted solution posted as an UPDATE within my question, but was admonished that it was bad etiquette and instructed to post an answer.
#ikegami's deserves the credit for the core of the solution.
sub get_jsonTree {
my ($array_of_hashes_ref) = #_;
my $roots;
my %recs_by_name;
my %children_by_parent_name;
my %count;
for my $row (#$array_of_hashes_ref) {
my $name = $row->{position_id};
my $parent_name = $row->{placement_id};
my $rec = {
name => $name,
};
## Added to loop through all key,value pairs and add them to $rec
while ( my ($key, $value) = each(%$row) ) {
$rec->{$key} = $value;
}
##Added To Count Child Nodes
$count{$parent_name} = 0 if (!$count{$parent_name});
$rec->{'child_count'} = $count{$parent_name};
$count{$parent_name}++;
push #{ $children_by_parent_name{$parent_name // 'root'} }, $rec;
$recs_by_name{$name} = $rec;
}
$roots = delete($children_by_parent_name{root}) || [];
for my $name (keys(%children_by_parent_name)) {
my $children = $children_by_parent_name{$name};
if ( my $rec = $recs_by_name{$name} ) {
$rec->{children} = $children;
} else {
$util{'test'} .= "Parent $name doesn't exist.\n<BR>";
push #$roots, #$children;
}
}
use JSON;
my $json_str = encode_json(#{$roots}[0]);
return $json_str;
}
my $array_of_hashes_ref = [
{ position_id => 123, placement_id => undef },
{ position_id => 456, placement_id => 123 },
{ position_id => 789, placement_id => 123 },
# ...
];
my $json_str = &get_jsonTree($array_of_hashes_ref);