nested hash

L

lyoute

hi,

i got a hash like:
$hello{key1s}->{key2s}->{'attr1'}
$hello{key1s}->{key2s}->{'attr2'}

i tried:

$hello{'a1'}->{'aa2'}->{'attr1'} = "abc1";
$hello{'a1'}->{'aa2'}->{'attr2'} = "abc2";
$hello{'a1'}->{'aabc2'}->{'attr1'} = "blah1";
$hello{'a1'}->{'aabc2'}->{'attr2'} = "blah2";
$hello{'b1'}->{'b2'}->{'attr1'} = "foo1";
$hello{'b1'}->{'b2'}->{'attr2'} = "bar2";

foreach $k1 (keys(%hello) ) {
print "$k1\n";
#i can get all the key1s, but how can i get key2s?
#i tried :
# foreach $k2 ( keys( %($hello{$k1}) ) {
# }
#but didn't work
}

how can i get the key2s?
 
G

Gunnar Hjalmarsson

lyoute said:
i got a hash like:
$hello{key1s}->{key2s}->{'attr1'}
$hello{key1s}->{key2s}->{'attr2'}

i tried:

$hello{'a1'}->{'aa2'}->{'attr1'} = "abc1";
$hello{'a1'}->{'aa2'}->{'attr2'} = "abc2";
$hello{'a1'}->{'aabc2'}->{'attr1'} = "blah1";
$hello{'a1'}->{'aabc2'}->{'attr2'} = "blah2";
$hello{'b1'}->{'b2'}->{'attr1'} = "foo1";
$hello{'b1'}->{'b2'}->{'attr2'} = "bar2";

foreach $k1 (keys(%hello) ) {
print "$k1\n";
#i can get all the key1s, but how can i get key2s?
#i tried :
# foreach $k2 ( keys( %($hello{$k1}) ) {
# }
#but didn't work
}

"Didn't work" is a useless description of your problem. When you ask
for help because something doesn't work, *always* state

- the expected output
- the output you actually got
- error and warning messages (enable strictures and warnings and have
Perl help you debug the code before asking other people for help)
how can i get the key2s?

From what I can see you didn't even try to print them. But, of
course, you need to fix the syntax errors, too.

Btw, have you read "perldoc perldsc"?
 
A

! aaa

lyoute said:
hi,

i got a hash like:
$hello{key1s}->{key2s}->{'attr1'}
$hello{key1s}->{key2s}->{'attr2'}

i tried:

$hello{'a1'}->{'aa2'}->{'attr1'} = "abc1";
$hello{'a1'}->{'aa2'}->{'attr2'} = "abc2";
$hello{'a1'}->{'aabc2'}->{'attr1'} = "blah1";
$hello{'a1'}->{'aabc2'}->{'attr2'} = "blah2";
$hello{'b1'}->{'b2'}->{'attr1'} = "foo1";
$hello{'b1'}->{'b2'}->{'attr2'} = "bar2";

foreach $k1 (keys(%hello) ) {

$k3=$hello{$k1};
foreach $k2 (keys(%$k3) ) {
print $k2;
}
 
J

Joe Smith

lyoute said:
foreach $k1 (keys(%hello) ) {
print "$k1\n";
#i can get all the key1s, but how can i get key2s?
#i tried :
# foreach $k2 ( keys( %($hello{$k1}) ) {
# }
#but didn't work
}

Change %() to %{}. That should be
foreach $k2 ( keys( %{$hello{$k1}} ) { ... }

-Joe
 

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,780
Messages
2,569,614
Members
45,287
Latest member
Helenfem

Latest Threads

Top