Hash of hashes

Discussion in 'Perl Misc' started by n00b, Apr 10, 2006.

  1. n00b

    n00b Guest

    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";
    }
    }
     
    n00b, Apr 10, 2006
    #1
    1. Advertising

  2. 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.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
     
    Michael Goerz, Apr 10, 2006
    #2
    1. Advertising

  3. n00b

    J. Gleixner Guest

    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::Dumper
     
    J. Gleixner, Apr 10, 2006
    #3
  4. n00b

    n00b Guest

    Thanks a million! Your posts really helped me out and I got it working
    now!
     
    n00b, Apr 11, 2006
    #4
  5. n00b <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 11, 2006
    #5
  6. n00b

    Andrew Guest

    Tad McClellan wrote:
    > n00b <> 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
     
    Andrew, Apr 13, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    595
    red floyd
    Nov 10, 2011
  2. Scott  Gilpin
    Replies:
    2
    Views:
    238
  3. Perl Learner

    Hashes of hashes or just one hash ?

    Perl Learner, Jun 8, 2005, in forum: Perl Misc
    Replies:
    11
    Views:
    234
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    237
  5. Replies:
    3
    Views:
    233
Loading...

Share This Page