in reply to Re^6: trying to decide best data structure for problem at hand.
in thread trying to decide best data structure for problem at hand.

I think the best help I can give you at this point is to send you back to school. ;)

Take a wander around the Tutorials campus, especially check out the Data Types and Variables department. The Data Type: Hash courses will be of particular interest. You will find the texts perldata, perlref and perldsc are required reading for those courses.

Enjoy your term back at school.


DWIM is Perl's answer to Gödel
  • Comment on Re^7: trying to decide best data structure for problem at hand.

Replies are listed 'Best First'.
Re^8: trying to decide best data structure for problem at hand.
by mikejones (Scribe) on Jan 08, 2007 at 21:11 UTC
    yeah...i do need to understand hashes better b/c I do not seem to get them, but I got it working:
    for my $i ( keys %dub_hash ) { my $key = "$i -- "; if (keys (%{$dub_hash{$i}}) > 1) { print $key, join( "\n" . ' ' x length $key, keys %{ $dub_hash{ $i } } ),"\n"; } }
    thanks again.
Re^8: trying to decide best data structure for problem at hand.
by mikejones (Scribe) on Jan 10, 2007 at 18:31 UTC
    ##-- Create a HoHoH. 1st hash keyed by name, that accesses 2nd hash k +eyed by UID, that accesses 3rd hash keyed by remaining items: gid,gec +os and hostname --## $dub_hash{$name}{$uid} = { # User data keyed by uid gid => $gid, gecos => $gecos, host => $host,
    So this code creating 3 hashes one with a key of name and the next with a key of uid and gid, gecos and host are values in the last hash? thank you

      Yes. dub_hash is a hash. {$name} accesses an element in the hash, creating it if it didn't exist already.

      {$name}{$uid} is shorthand for {$name}->{$uid} which implies the value accessed by $name is a hash reference. Again the hash element is generated if it didn't exist already.

       = {...}; is an annonymous hash whose reference is assigned to the element accessed by {$name}{$uid}.

      The result is a HoHoH - hash of hash of hash.


      DWIM is Perl's answer to Gödel
        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. thank you...again and the last ?