You have already been given good answers, but I thought I could share here some code that I wrote for an answer I made about five days ago to a similar question on another forum (
http://perlguru.com/gforum.cgi?post=80741;guest=19041527. It provides a very sketchy and incomplete prototype implementation of a
dumper function somewhat similar to the
Data::Dumper module (but much less advanced). The data sample used in this example is not yours, but you could very easily replace the content of the
$response variable below with your own data sample.
use strict;
use warnings;
my $response = {
'items' => [],
'messages' => [],
'status' => 1,
'total_results' => 1,
'xmlref' => {
'Items' => [
{
'ASIN' => 'B00PXMMHMY',
'BrowseNodes' => {
'BrowseNode' => [
{
'Ancestors' => {
'BrowseNode' => {
'BrowseNodeId' => '3146281',
'Name' => 'Home & Garden'
}
},
},
{
'Ancestors2' => {
'BrowseNode2' => {
'BrowseNodeId' => '3146282',
'Name' => 'Garden & home'
}
},
}
]
}
}
]
}
};
dumper (0, ref $response, $response);
sub dumper {
my ($level , $ref_type_in, $reference) = @_;
if ($ref_type_in eq 'ARRAY' ) {
for my $value (@$reference) {
my $ref_type_out = ref $value;
if ($ref_type_out) {
dumper ($level + 1, $ref_type_out, $va
+lue)
} else {
print "\t" x $level; print $value, "\n
+";
}
}
} elsif ($ref_type_in eq 'HASH' ) {
for my $key (keys %$reference) {
print "\t" x $level, $key, " => ";
my $value = $reference->{$key};
my $ref_type_out = ref $value;
if ($ref_type_out) {
print "\n";
dumper ($level + 1, $ref_type_out, $va
+lue)
} else {
print $value, "\n";
}
}
} else {
print "Unknown reference type\n";
}
}
The output is far from being as rich as
Data::Dumper and offers a lot of opportunities for improvement, but let's say that we have a reasonably good proof of concept. My aim for this post on the other forum was anyway not to write a replacement for
Data::Dumper, but only to demonstrate how to traverse a nested data structure in which the nodes might be hash or array references.
xmlref =>
Items =>
ASIN => B00PXMMHMY
BrowseNodes =>
BrowseNode =>
Ancestors =>
BrowseNode =>
BrowseNodeId => 3146281
Name => Home & Garden
Ancestors2 =>
BrowseNode2 =>
BrowseNodeId => 3146282
Name => Garden & home
messages =>
total_results => 1
status => 1
items =>
And, BTW, my approach is also recursive. I do not think that I need to repeat how recursion is important, others have stated it quite clearly.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.