Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hash of hashes

Reply
Thread Tools

Hash of hashes

 
 
n00b
Guest
Posts: n/a
 
      04-10-2006
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";
}
}

 
Reply With Quote
 
 
 
 
Michael Goerz
Guest
Posts: n/a
 
      04-10-2006
n00b wrote:
> 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.h...ing-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
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      04-10-2006
n00b wrote:
> 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:umper
 
Reply With Quote
 
n00b
Guest
Posts: n/a
 
      04-11-2006
Thanks a million! Your posts really helped me out and I got it working
now!

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-11-2006
n00b <(E-Mail Removed)> wrote:

> 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.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Andrew
Guest
Posts: n/a
 
      04-13-2006
Tad McClellan wrote:
> n00b <(E-Mail Removed)> wrote:
>
> > 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.
>


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
"Pseudo-hashes are deprecated" error and accessing a hash of hashes ernestm@mindspring.com Perl Misc 3 01-31-2006 04:40 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of hashes or just one hash ? Perl Learner Perl Misc 11 06-09-2005 06:55 AM
Performance Improvement of complex data structure (hash of hashes of hashes) Scott Gilpin Perl Misc 2 08-26-2004 01:02 AM



Advertisments