whaled has asked for the wisdom of the Perl Monks concerning the following question:

$drw{dave}{one} = "done"; $drw{dave}{two} = "dtwo"; $drw{dave}{three} = "dthree"; $drw{eileen}{one} = "eone"; $drw{eileen}{two} = "etwo"; $drw{eileen}{three} = "ethree"; foreach $f (keys %drw) { print "f($f)\n"; } # ????? how do I find whats in the second hash key set {} # foreach $f (keys %drw{eileen}) { # print "f($f)\n"; # }
I am looking how to be able to list the keys on the second hash gate do you know how I might do this. The second loop I would hope to receive "eone", "etwo", "ethree"

Replies are listed 'Best First'.
Re: Double Hash Key
by mcogan1966 (Monk) on Jan 15, 2004 at 20:07 UTC
    You're close, but the loop should be like this:
    foreach $f ( keys %{$drw{eileen}) { print "f($f)\n"; }
    Direcly out of the O'Reilly "Programming Perl" book, page 271.
Re: Double Hash Key
by CountZero (Bishop) on Jan 15, 2004 at 21:45 UTC
    Perhaps a word of explanation is advised here, so you know where you went wrong (since you were oh so close to the solution).

    The structure you have made with the %drw-hash is as follows:

    $VAR1 = { 'dave' => { 'three' => 'dthree', 'one' => 'done', 'two' => 'dtwo' }, 'eileen' => { 'three' => 'ethree', 'one' => 'eone', 'two' => 'etwo' } };
    (Data::Dumper is a very useful module to show you the structure of your arrays, hashes and objects -- do have a look at it)

    As you see, the keys "dave" and "eileen" point to another hash-structure, it means they are references to another hash.

    Now to access a variable (scalar, array, hash) through a reference you have to dereference the reference, which results in turning the reference back into the variable it pointed to.

    Dereferencing is easy: just put the magic $, @ or % in front of the reference and the reference turns into the variable of the kind indicated by the prefix (of course you cannot turn a reference for a scalar into a hash, ...; you have to respect the type of the reference!)

    That is why you cannot say %drw{eileen} (Which is wrong in any case, at least it should be $drw{eileen}) and expect it to give you access to the keys and values of the second level hash (Try it and you will see you get an answer like HASH(0x1c2522c), indicating you were dealing with a reference to a hash), but have to dereference it so you can get to the second level of the Hash-of-Hashes: %{$drw{eileen}}

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Double Hash Key
by derby (Abbot) on Jan 15, 2004 at 21:09 UTC
    foreach $f (keys %drw) { foreach $g ( keys %{$drw{$f}} ) { print "$f/$g f($drw{$f}{$g})\n"; } }

    -derby

Re: Double Hash Key
by Roy Johnson (Monsignor) on Jan 15, 2004 at 20:26 UTC
    perldoc perlreftut

    The PerlMonk tr/// Advocate
Re: Double Hash Key
by 3dbc (Monk) on Jan 15, 2004 at 21:13 UTC
    recon this should accomplish what you are asking for:
    $href = \%drw; foreach ( keys %$href) { print "primaryKey = $_\n"; foreach ( keys %{$href->{$_}}) { print "\tsubKey = $_\n"; } }

    However your question is a bit ambiguous, I quote “am looking how to be able to list the keys on the second hash gate do you know how I might do this. The second loop I would hope to receive "eone", "etwo", "ethree"” If you want to receive "eone", "etwo", "ethree"” these are the values of the subkeys. Which one do you want Keys or Values of subkeys? If you indeed would like the values use the below code:
    $href = \%drw; foreach ( keys %$href) { print "primaryKey = $_\n"; foreach ( values %{$href->{$_}}) { print "\tsubKeyValue = $_\n"; } }

    -3DBC ;-)
      Why are you taking a ref to the hash?

      The PerlMonk tr/// Advocate
        Why Not? It adds another level of data abstraction. The entire data structure can now be considered an object. Isn't a complex data structure pointer considered to be better programming style?

        Thanks,
        -3dbc