in reply to Traversing a hash of hashes of hashes

Hello kcorj2244 and welcome to the monastery and to the wonderful world of Perl!

you'll find perldsc an useful read for sure. But the task is not so complex: you can use some usful functions Perl offers to you. For exmple ref can tell you if a value you got fetching some key is a scalar or a hash ref. In the latter case you just to iterate over it as you've already done for the container hash.

use strict; use warnings; my %h1=('qwqw' => {'qw' => 'qww','df' => 'dfff'},); my %hash=( 'AAA' => 'aaa', 'BBB' => 'bbb', 'CCC' => {'A1' => 'a1','B1' => 'b1','C1' => {'A2' => 'a2','B2' => 'b2' +,'C2' => 'c2END'}}, 'DDD' => 'ddd', 'EEE' => \%h1, 'FFF' => 'sdsfsds', ); ddump (\%hash); sub ddump { my $ref = shift; my $deep = shift||0; foreach my $k (sort keys %{$ref}) { if (ref( ${$ref}{$k})) {print "\t" x $deep."$k =>\n"; &dd +ump (${$ref}{$k}, ($deep+1))} # key and value # else {print "\t" x ($deep)."$k => ${$ref}{$k}\n";} # or just the key as your need else {print "\t" x ($deep)."$k\n";} } } # OUTPUT AAA BBB CCC => A1 B1 C1 => A2 B2 C2 DDD EEE => qwqw => df qw FFF

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Traversing a hash of hashes of hashes
by kcorj2244 (Novice) on Mar 29, 2017 at 11:38 UTC
    Thank you for your response. Is there something special that's required to deference my $bigHash? I believed that %{$bigHash} would suffice, but %bigHash is only populated with the first hash. I don't have direct access to %bigHash just in case you were wondering why I was asking this. I took a look at the perldoc for this, and what I found only resulted in the first hash being in there.
      hello,

      extra braces are genally not needed to do the dereference

      # plain hash note parens() my %plain = (a=>1,b=>2); # an hash reference note braces {} my $ref = {c=>3,d=>4}; #dereferencing example foreach my $k ( keys %$ref ) # is the same of foreach my $k ( keys %{$ref} )

      Generally braces in %{$ref} are not needed because perl knows that there is only one possible way to dereference it.

      For more example see tye's tutorial References quick reference

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Generally braces in %{$ref} are not needed because perl knows that there is only one possible way to dereference it.

        Generally true, but ... IMO it's better to use the curly braces for clarity, and also because then you can dereference the result of any expression inside the block enclosed by them. This helps to avoid errors caused by attempting to dereference undefined structures, for example. Here's a simplified example (most often, I find, this happens when trying to dereference the value of a hash key that may or may not be defined):

        $ perl -Mwarnings -Mstrict -MData::Dumper -E' my @foo = ( [qw/ a b c /], undef ); say "@$_" for @foo; ' a b c Can't use an undefined value as an ARRAY reference at -e line 3.
        $ perl -Mwarnings -Mstrict -MData::Dumper -E' my @foo = ( [qw/ a b c /], undef ); say "@{ $_ // [] }" for @foo; ' a b c
        It's not necessary all the time, but like most things, if you are going to need it sometimes, you might as well develop the habit through consistency.

        IIRC the Perl docs even recommend to always use the braces, but I can't find the "reference" now.

        Hope this helps!


        The way forward always starts with a minimal test.
        Understood. However I face a strange issue with this: Here is an example. $bigHash is similar to my example. A hash full of other hashes full of other hashes and so forth. I don't have direct access to %bigHash only $bigHash. So, me wanting to dereference it would assume that my %newHash = %$bigHash would do the trick right? The issue is, is %newHash only contains the FIRST hash of $bigHash. I'm not entirely sure on what causes this, but I'd love to learn!