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

I ultimately want to traverse a hash of unknown topology to determine if I have reached leaf nodes (no hashes exist beneath the current reference). The following test code:

#!/usr/bin/perl use strict; my %hash; print "'hash' is a hash reference\n" if ref(\%hash) eq 'HASH'; $hash{key} = 'value'; print "'hash' is still a hash reference\n" if ref(\%hash) eq 'HASH'; print "'hash{key}' is a scalar reference\n" if ref(\$hash{key}) eq 'SC +ALAR'; print "'hash{key}' is a hash reference\n" if ref(\%{$hash{key}}) eq 'H +ASH';
gives me the error Can't use string ("value") as a HASH ref while "strict refs" in use when I try to test $hash{key} for a type that it isn't on the final line.

How do I go about testing each node in the tree to determine whether I can go further down the structure?

Thanks.

Replies are listed 'Best First'.
Re: hash reference syntax?
by tachyon (Chancellor) on Mar 06, 2004 at 00:06 UTC

    The problem is recursive. Does this help?

    my $h = { key1 => 'val1', key2 => { hash_ref2 => 'val2' }, key3 => { hash_ref3 => { deeper => 'deep' , real_deep => { diving => 'now' } }, }, key4 => [ 'array ref1', 'array_ref2' ], }; use Data::Dumper; print Dumper $h; rec_tree( $h ); sub rec_tree { my $val = shift; if ( ref($val) eq 'HASH' ) { rec_tree( $_ ) for values %$val; } elsif ( ref($val) eq 'ARRAY' ) { print "@$val\n"; } else { # assume scalar as we are not dealing with other case # will stringify exceptions..... print $val, $/; } } __DATA__ $VAR1 = { 'key1' => 'val1', 'key2' => { 'hash_ref2' => 'val2' }, 'key3' => { 'hash_ref3' => { 'real_deep' => { 'diving' => 'n +ow' }, 'deeper' => 'deep' } }, 'key4' => [ 'array ref1', 'array_ref2' ] }; val1 val2 now deep array ref1 array_ref2

    cheers

    tachyon

      Thanks for your answer. Yes, I was ultimately thinking of implementing the solution through recursion -- which raises the following question: what does the Perl interpreter do when the stack is exceeded? Thanks.
        what does the Perl interpreter do when the stack is exceeded?

        The stack won't overflow (though you could use up all available memory if things really get out of hand). However, Perl does issue a warning by default if you exceed some predetermined level of recursion. If I'm not mistaken, the predetermined level is decided upon when Perl itself is built for your particular system. In other words, you can increase the number if you want to rebuild your implementation of Perl.

        Take a look at the following snippet... it should produce the warning, which on my system (and most implementations of Perl) occurs at the 100th level of recursion:

        use strict; use warnings; sub recurse { my $value = shift; recurse ( $value - 1 ) if $value > 0; print $value, "\t"; } recurse( 99 );

        And the output is: "Deep recursion on subroutine "main::recurse" at .....", followed by the list of recursion levels (that part is because of the snippet's print call).

        You can turn off recursion warnings by saying "no warnings qw(recursion);" within the scope of your recursive function.

        Hope this helps...


        Dave

Re: hash reference syntax?
by Wonko the sane (Curate) on Mar 06, 2004 at 00:15 UTC
    Hello,

    The reason you are getting that error is just like the error says.
    you are trying to use the string 'value' as a hash reference.

    print "'hash{key}' is a hash reference\n" if ref(\%{$hash{key}}) eq 'H +ASH';
    $hash{key} = 'value'

    and you are trying to reference it as a hash by surrounding it with '\%{ }
    In Essense you are saying '\%{"value"}'

    I'm not really sure what you are doing with this, but you could use some sort of
    recursive subroutine to dig down into the hash to get what you want.

    Something like this maybe.

    #!/usr/local/bin/perl5.6.0 use strict; use warnings; my $hash = { h1 => { ss1 => 'Howdy' }, h2 => { hh1 => { sss1 => 'Hello' } }, s1 => 'Doody', a1 => [], }; dig( $hash ); sub dig { my ( $hole ) = @_; if( ref($hole) eq 'HASH' ) { foreach my $l ( keys %{$_[0]} ) { dig( $_[0]->{$l} ); } } elsif ( ref($hole) eq 'ARRAY' ) { print q{What do I do with an Array ref?\n}; } else { my $leaf = $hole; # leaf in a hole?!? print qq{Found a leaf in an hole! [$leaf]\n}; } }
      Thanks for both of your answers. My error was that I was testing only against 'HASH' while I should have given myself an out. Taking your answers a step further, when I apply all types mentioned for ref() at Perldoc.com as in the following:
      #!/usr/bin/perl use strict; sub traverse($); #my $hash = { # key0 => 'value0', # key1 => { key2 => 'value2' }, # key3 => { key4 => 'value4', key5 => { key6 => 'value6' } } #}; my %hash; $hash{key0} = 'value0'; $hash{key1}{key2} = 'value2'; $hash{key3}{key4} = 'value4'; $hash{key3}{key5}{key6} = 'value6'; traverse(\%hash); sub traverse($) { my $r = shift; if (ref($r) eq 'HASH') { for my $v (values %$r) { traverse($v); } } elsif (ref($r) eq 'SCALAR') { print "SCALAR:\t'$r'\n"; } elsif (ref($r) eq 'ARRAY') { print "ARRAY:\t'$r'\n"; } elsif (ref($r) eq 'REF') { print "REF:\t'$r'\n"; } elsif (ref($r) eq 'LVALUE') { print "LVALUE:\t'$r'\n"; } elsif (ref($r) eq 'CODE') { print "CODE:\t'$r'\n"; } elsif (ref($r) eq 'GLOB') { print "GLOB:\t'$r'\n"; } else { print "unexpected value:\t'$r'\n" } }

      I get the following unexpected (at least to me...) results:

      unexpected value: 'value2' unexpected value: 'value0' unexpected value: 'value6' unexpected value: 'value4'
      How is this possible if I have accounted for all possible types? Thanks.

        How is this possible if I have accounted for all possible types?

        'some string' is a literal. It is not a reference to anything. I presume you though that ref() would report scalar.

        $foo = 'bar'; print '$foo is a ', ref($foo), $/; print '\$foo is a ', ref(\$foo), $/; __DATA__ $foo is a \$foo is a SCALAR

        cheers

        tachyon