3 layer hash

N

none

Hi. I am sweating aftre hours of trial and error and hope you can
help me please:)



3 layer hash

I need to access the 3rd layer , I have the key value from the first
and third layers but not from the second

I thought the following would get me the second layer

$xml->{name1}->[0]->{name3}

but doesnt
the keys in layer 1 and 3 are constant.. the changing key is layer 2.
i need to loop through without knowing..


please point me in right direction... help ... thank you :)
 
N

non

:) the keys in layer 1 and 3 are constant.. the changing key is layer 2.
:) i need to loop through without knowing..


I'd use 'each':

while (my ($key, $value) = each %{$hash {name1}}) {
my $element = $$value {$name3};
...
}


Thank you that works great :)

I have
x1 {name1}
->x? {unknown}
-> x5 {name3}

Your answer loops through main1 which i only have 1 or does it go
through each layer automatically and simply store all keys-values


$$value {$name3} works perfectly but how does it know to get the data
from layer three... suppose the key-value of name3 also exists in
name2 ?



I apologise for the no brainers... but i would like to learn as well
as use the answer... please :)
 
N

non

O
I apologise for the no brainers... but i would like to learn as well
as use the answer... please :)


Because I need to know the value of the key ie {name2} (as well). can
you help with that please?
 
X

Xicheng

Hi. I am sweating aftre hours of trial and error and hope you can
help me please:)
3 layer hash
Unless you provide us with your data structure and how you defined
$xml, it's not easy to reach a proper solution.
I need to access the 3rd layer , I have the key value from the first
and third layers but not from the second
I thought the following would get me the second layer

$xml->{name1}->[0]->{name3}
I assume that you were using hash reference $xml = {....} instead of
%xml=(...). then $xml->{name1}->[0]->{name3} is the same as
$xml->{name1}[0]{name3}, the second layer is an array, you get a single
value from the hash? If it's a %xml, you've got to use
$xml{name1}[0]{name3} to access this 3rd layer element..
but doesnt
the keys in layer 1 and 3 are constant.. the changing key is layer 2.
i need to loop through without knowing..
I suppose you are trying to go through all elements under
$xml->{name1}, then you may try something like this:

for my $e(0..$#{$xml->{name1}}) {
for my $k(keys %{$xml->{name1}[$e]}){
print "$k=>$xml->{name1}[$e]{$k}\t";
}
print "\n";
}

Xicheng
 
X

Xicheng

O


Because I need to know the value of the key ie {name2} (as well). can
you help with that please?

If you just want to know the structure of your datatype, no need to
re-invent the wheel, try one of the most popular CPAN modules
Data::Dumper..
====
use Data::Dumper;
.......
print Dumper $xml;
====
Xicheng
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top