Deeper level of hashing

D

dn.perl

I am using (roughly) the following code.

my %counter = () ;

$counter{label11}{parent} = 'label1' ;
$counter{label11}{hits} = 14 ;
$counter{label12}{parent} = 'label1' ;
$counter{label12}{hits} = 14 ;

If I use: for my $key(keys %counter), I get label11 and label12 as
values.
I would like to run something like: foreach my $key(keys %
($counter(label11) ) )
and get 'parent' and 'hits' as the sub-keys.

Is there any quick way to do this but quicker than what? So let's say
is there a 'standard way' in which this is done? Or do I have to do it
the 'hard way' ? Define a sub-hash, and then assign it to the parent
hash as its key's value?
 
J

Joost Diepenmaat

I am using (roughly) the following code.

my %counter = () ;

$counter{label11}{parent} = 'label1' ;
$counter{label11}{hits} = 14 ;
$counter{label12}{parent} = 'label1' ;
$counter{label12}{hits} = 14 ;

If I use: for my $key(keys %counter), I get label11 and label12 as
values.
I would like to run something like: foreach my $key(keys %
($counter(label11) ) )
and get 'parent' and 'hits' as the sub-keys.

Is there any quick way to do this but quicker than what? So let's say
is there a 'standard way' in which this is done? Or do I have to do it
the 'hard way' ? Define a sub-hash, and then assign it to the parent
hash as its key's value?

Do you mean something like this?

foreach my $key (keys %{$counter{label11}}) {
print "$key => $counter{label11}->{$key}\n";
}
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top