in reply to Re^2: Syntax for Hashes of Hashes
in thread Syntax for Hashes of Hashes

To define an inner hash without contents, you can do:
$g{$name} = {};
to check for existence, you do:
if ( exists($g{$name} ) { }
Otherwise, the way you were doing it, you are only checking to see if the value of the key is defined with a true value.

Replies are listed 'Best First'.
Re^4: Syntax for Hashes of Hashes
by pg (Canon) on Jul 24, 2005 at 21:14 UTC

    However be careful with exists(), it creates stuff for you:

    use Data::Dumper; use strict; use warnings; my $h = {}; if (exists($h->{"a"}{"b"})) { } if (exists($h->{"a"})) { print "exists\n"; } print Dumper($h);

    This will make $h->{"a"} exists.

Re^4: Syntax for Hashes of Hashes
by o2bwise (Scribe) on Jul 24, 2005 at 21:11 UTC
    hubb0r,

    Thanks, man!

    Say, got one more question. I am trying to figure out the syntax for printing out the values of the inner hash OUTSIDE the subroutine.

    Here is as far as I am (I pushed all name values in an array called @names):

    print "From outside the subroutine:\n"; my $i = 0; while ( $i < @names ) { print "$myHash{ $names[$i] }{ name }\n"; $i++; }
    and I have this error:

    Use of uninitialized value in concatenation (.) or string at hashOfHas +hes.pl line 22, <INFO> line 5.
    Line 22 is the print statement within the while loop (figures).

    Thanks again...

    Tony
      Personal preference, but I would write it like:
      foreach my $key (@names) { print $myHash{$key}{'name'}, "\n"; }
      Of course, that would mean that at some point you would have had to assign a hash value with the key 'name' to each nested hash.
        Hey hubb0r,

        Sure appreciate the assistance, but I don't think this one works. I'll supply the code. It just keeps failing for the same reason:

        Use of initialized value in print at hashOfHashes.pl line 22, <INFO> line 6.

        Here's the code and Line 22 is the print statement(by the way, the key values successfully print out):

        #!/usr/bin/perl -w use strict; my %myHash = (); my @names; open (INFO,"<info.txt"); while (<INFO>) { my ($name,$species,$gender,$age,$hairColor) = split /,/,$_; push @names,$name; $myHash{ $name } = {}; addValues( $myHash{ $name },\$name,\$species,\$gender,\$age,\$hair +Color); } print "From outside the subroutine:\n"; foreach my $key ( @names ) { print "Key:\t$key\n"; print $myHash{$key}{'name'}, "\n"; } close (INFO); sub addValues { my ( $hashNameRef,$namRef,$speRef,$genRef,$ageRef,$hairColRef ) = +@_; $hashNameRef = { name => $$namRef, species => $$speRef, gender => $$genRef, age => $$ageRef, hairColor => $$hairColRef }; print "$$hashNameRef{'name'}\t$$hashNameRef{'species'}\t$$hashName +Ref{'gender'}\t$$hashNameRef{'age'}\t$$hashNameRef{'hairColor'}\n"; }
        And again, here is the data file:

        Tony,human,male,47,gray Mark,human,male,48,brown Tyler,dog,male,11,brown Anthi,human,female,39,black Spud,cat,male,14,white Erica,human,female,23,blonde
        Am I doing something wrong? I don't know, man! I tried a lot of syntax variants, but no go. I always get that error message.

        Tony (o2bwise)