"I want to get just the values of each child for a certain parent""How do I get the key values for the first branch (parent id) of the hash?"
Note that the values for each key for parents is actually a reference and so is the value for each child key(a, b, c) in this data structure... If you meant by values that you want the IDs for each child of a certain parent, as I understood, then I hope the following excerpt of code can be of help.
#!/usr/local/bin/perl
use strict;
use warnings;
my %children;
#each parent has 3 children identified by their IDs
$children{'parent1'}{'a'}{'id'} = 'Child1P1';
$children{'parent1'}{'b'}{'id'} = 'Child2P1';
$children{'parent1'}{'c'}{'id'} = 'Child3P1';
$children{'parent2'}{'a'}{'id'} = 'Child1P2';
$children{'parent2'}{'b'}{'id'} = 'Child2P2';
$children{'parent2'}{'c'}{'id'} = 'Child3P2';
for my $parent (sort keys (%children)){
print "The key value is a reference of type: ", ref $children{$par
+ent}, $/;
print "The parent is $parent:\n";
for my $child (keys (%{$children{$parent}})){
print "children of $parent-> $children{$parent}{$child}{'id
+'}\n";
}
print "\n\n";
}
#OUTPUT:
The key value is a reference of type: HASH
The parent is parent1:
children of parent1-> Child3P1
children of parent1-> Child1P1
children of parent1-> Child2P1
The key value is a reference of type: HASH
The parent is parent2:
children of parent2-> Child3P2
children of parent2-> Child1P2
children of parent2-> Child2P2
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.
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.