Sorry I didn't include the code I have been trying. Here it is. This is the data set I have been using.
$rh_dtree = { 'L1' => { 'L2b' => { 'L2b1' => {}, 'L2b2' => {} }, 'L2a' => { L3b' => { L4a' => {} }, L3a' => {} } }, 'L3b' => { 'L4a' => {} }, 'L2b' => { 'L2b1' => {}, 'L2b2' => {} }, 'L2a' => { 'L3b' => { 'L4a' => {} }, 'L3a' => {} } };
I have changed the code many times. Here is my latest attempt.
Main(); Main { my %dtree = init(); processDtree(\%dtree); } sub processDtree { my $rh_dtree = shift; foreach my $parent ( keys %{$rh_dtree} ) { my @nodenames = (); hash_walk( \%{$rh_dtree->{$parent}},[],\@nodenames); print join("\n",@nodenames) . "\n"; exit; } } sub hash_walk { my ($rh_dtree,$ra_keys,$ra_nodenames) = @_; while (my ($k, $v) = each %$rh_dtree) { # keep keys ordered. push(@$ra_keys,$k); if ( scalar keys %{$v} ) { hash_walk($v,$ra_keys,$ra_nodenames); } } push(@$ra_nodenames, join(".",@$ra_keys)); @$ra_keys = (); }

The output from running the follows: Note that I am exiting in the first iteration of the foreach loop in processDtree to make it easier to examine the output.

L2b.L2b1 L2b.L2b1.L2b2 L2b.L2b1.L2b2 L2b.L2b1.L2b2.L2a.L3b.L4a L2b.L2b1.L2b2.L2a.L3b.L4a L2b.L2b1.L2b2.L2a.L3b.L4a.L3a L2b.L2b1.L2b2.L2a.L3b.L4a.L3a
I would like to achieve this:
nested-L2b nested-L2b.nested-L2b1 nested-L2b.nested-L2b2 nested-L2a nested-L2a.nested-L3b nested-L2a.nested-L3b.nested-L4a nested-L2a.nested-L3a

In reply to Re^2: walking a hash by rjl
in thread walking a hash by rjl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.