in reply to hash reference syntax?

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

Replies are listed 'Best First'.
Re: Re: hash reference syntax?
by Anonymous Monk on Mar 06, 2004 at 00:53 UTC
    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