Hash of hashes

N

n00b

Hello,

I've been trying to program a hash of hashes, where the "sub hashes"
would be created at runtime, but with no luck so far. I don't really
know Perl, and I'm a bit lost with the notation - when to use % and
when just $.
Here's an example of what I'm trying to do - that is, I would want this
to print out the keys and values of the sub-hashes - could somebody
tell me why it doesn't work?

Thanks!

my %master_hash = ();
my $count = 0;

for($count=1; $count<11; $count++)
{
my %sub_hash =
(
key1 => $count,
key2 => $count,
key3 => $count,
);
$master_hash{$count} = %sub_hash;
}

my %temp_hash = ();

while( my ($k, %v) = each %master_hash )
{
print "master key: $k \n";
%temp_hash = %v;

while( my ($kk, $vv) = each %temp_hash )
{
print "sub key: $kk value: $vv\n";
}
}
 
M

Michael Goerz

n00b said:
Hello,

I've been trying to program a hash of hashes, where the "sub hashes"
would be created at runtime, but with no luck so far. I don't really
know Perl, and I'm a bit lost with the notation - when to use % and
when just $.
Read perlreftut (http://perldoc.perl.org/perlreftut.html#Making-References)

You need to use references. In short, a hash of hashes can simply by
created like this:

my %keys = ();
$keys{"father"}->{"child"} = $value

Printing out the "sub hashes" should work like this:

while ((my $key, my $child) = each %keys) {
while ((my $childkey, my $childvalue) = each %{$child}){
print "$childkey = $childvalue\n";
}
}

Best Wishes,
Michael
 
J

J. Gleixner

n00b said:
Hello,

I've been trying to program a hash of hashes, where the "sub hashes"
would be created at runtime, but with no luck so far. I don't really
know Perl, and I'm a bit lost with the notation - when to use % and
when just $.
Here's an example of what I'm trying to do - that is, I would want this
to print out the keys and values of the sub-hashes - could somebody
tell me why it doesn't work?

Thanks!

my %master_hash = ();
my $count = 0;

for($count=1; $count<11; $count++)
{
my %sub_hash =
(
key1 => $count,
key2 => $count,
key3 => $count,
);
$master_hash{$count} = %sub_hash;
}

my %temp_hash = ();

while( my ($k, %v) = each %master_hash )
{
print "master key: $k \n";
%temp_hash = %v;

while( my ($kk, $vv) = each %temp_hash )
{
print "sub key: $kk value: $vv\n";
}
}

my %master_hash;
#for($count=1; $count<11; $count++)
for my $count ( 1 .. 10 )
{
my %sub_hash =
(
key1 => $count,
key2 => $count,
key3 => $count,
);
$master_hash{$count} = \%sub_hash;
}

while( my ($k, $v) = each %master_hash )
{
print "k=$k\n";
#$v is a reference to a hash, dereference it..
while( my ($kk, $vv) = each %{$v} )
{
print "sub key: kk=$kk value: vv=$vv\n";
}
}


Since you seem to be after some sort of order, you probably want an
Array of Hashes (AoH), instead, or sort the keys of your master_hash,
accordingly.

For very good examples and explanations of useful data structures,
see:
perldoc perldsc

Also, to easily view your data structure you create, see:

perldoc Data::Dumper
 
T

Tad McClellan

n00b said:
and I'm a bit lost with the notation - when to use % and
when just $.


Use % when you want to refer to the entire hash, ie. when you
want to refer to all of the key/value pairs as a whole.

Use $ when you want to refer to a single thing, such as one
of the values in a hash.
 
A

Andrew

Tad said:
Use % when you want to refer to the entire hash, ie. when you
want to refer to all of the key/value pairs as a whole.

Use $ when you want to refer to a single thing, such as one
of the values in a hash.

Caution regarding both English and, consequently, Perl semantics :

A hash is, arguably, a "single thing", and this rather messes up the
above stated rule.
(not sure if the singular (English) word "value" is ever used to denote
multiple values in Perl (a Perl list) -- ("An array value"?), but,
regardless... )

I would refer to a sub-hash of a HoH with a "%", even though the
sub-hash can be construed as a "single thing" (in English, if not in
Perl)

Anyhow, why not say:

A prepended "$" is used for retrieving (referring to) a SCALAR value,
regardless of how many hash-key and/or array-subscript references
separate the said value from the "top", named variable.

Brief and true, on all levels, in all cases (for multi-dimensional
arrays, multi-level hashes and hash-array hybrids)

This also wonderfully loops back to and is consistent with the syntax
for the primitive scalar variable -- the simple $foo .

Andrew
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top