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

HoH -------- 'jblow' => { 'uid' => '2195', 'gecos' => 'Joe Blow,,,', 'gid' => '20' }, snippet..... ( $name, $p, $uid, $gid, $gecos, $dir, $s ) = split( ':' ); $hash1{$name} -> {'uid'} = $uid; $hash1{$name} -> {'gid'} = $gid; $hash1{$name} -> {'gecos'} = $gecos; HoHoH ---------- $VAR1 = { 'jblow' => { '2195' => { 'gecos' => 'Joe Blow,,,', 'gid' => '20' } } }; snippet...... $hash2{$name}{$uid} = { gid => $gid, gecos => $gecos, };
I am now comparing so I grasp hashes and understand them more, in the first printed dump of HoH, there are 2 hashes called "hash1 and "name". hash1 has a key of name with values of uid,gid and gecos? uid,gid and gecos are accessed only through name, joe blow in this case? In the printed dump of HoHoH, there are 3 hashes called "hash2", "name" and "uid". hash2 has keys of name and uid with values of gid and gecos? gid and gecos are only accessible through hash uid? please provide any corrections in detail. thank you...

Replies are listed 'Best First'.
Re: Comparing HoH and HoHoH
by liverpole (Monsignor) on Jan 17, 2007 at 15:34 UTC
    Hi mikejones,

    Not sure exactly which parts are giving you trouble, but note that "joe blow" is NOT how the uid, gid and gecos are being accessed.  "joe blow" is simply the value of the key gecos in the subhash.  The subhash is, itself, the value of the key jblow in the hash called $hash1.

    Part of your confusion may be that you're mixing hashes with hash references; the topmost hash, $hash1, is a normal hash, since you're doing the equivalent of:

    my %hash1 = ( ); # %hash1 is a normal hash (note the '%' prefix) $hash1{$name} -> {'uid'} = $uid; $hash1{$name} -> {'gid'} = $gid; $hash1{$name} -> {'gecos'} = $gecos;

    rather than:

    my $hash1 = { }; # $hash1 is a hash reference (note the '$' prefi +x) $hash1 -> {$name} -> {'uid'} = $uid; $hash1 -> {$name} -> {'gid'} = $gid; $hash1 -> {$name} -> {'gecos'} = $gecos;

    But then the key 'jblow' has as its value a hash reference, where the -> notation is used.

    In the case of:

    $VAR1 = { 'jblow' => { '2195' => { 'gecos' => 'Joe Blow,,,', 'gid' => '20' } } };

    you simply have another level deep, since the key '2195' has as its value a reference to another hash (that of { 'gecos' => 'Joe Blow,,,', 'gid' => '20' }) instead of a simple scalar.

    You could even combine the two, and have:

    $VAR1 = { 'jblow' => { 'uid' => '2195', 'gecos' => 'Joe Blow,,,', 'gid' => '20', '2195' => { 'gecos' => 'Joe Blow,,,', 'gid' => '20' } } };

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Hello...no problem per say just trying to grasp these data structures. How to recognize them and the differences between them, how to use them effectively, and how to read them. thanks
Re: Comparing HoH and HoHoH
by eff_i_g (Curate) on Jan 17, 2007 at 15:29 UTC
    hash1 has a key of name with values of uid,gid and gecos? uid,gid and gecos are accessed only through name, joe blow in this case
    The bold portion is correct. The name's value is a reference to an anonymous hash, which has the keys of uid, gid, and gecos, who point to their respective values.

    Every hash has key/value pairs; it's what the values contain--another hash, for instance--that make them powerful.

    Here's another way to look at it: every time you see the curly braces with a name, think "key of"; so, $hash2{$name}{$uid} results in $hash2 with a key of $name, then the key of $name with a key of $uid, which yields the value of $uid.

    Now, based off of this information, can you explain the 2nd hash again?

    Update: A little rewording.
      ok got it now. thanks