Hello,

I want to iterate over each node of a nested hash.

I learnt from you perlmonks how to iterate over all leaves of a nested hash with Data::Leaf::Walker.

I like this way a lot. So I ask you if there is a similar way to iterate over all nodes of a nested hash and not only over its leaves.

Here a code example:

use strict; use warnings; use Data::Leaf::Walker; my %nested_hash = ( a => { aa => {aaa => 5, aab => 5, aac => 5}, ab => {aba => 5, abb => 5, abc => 5} }, b => { ba => {baa => 5, bab => 5, bac => 5}, bb => {bba => 5, bbb => 5, bbc => 5} } ); my $walker = Data::Leaf::Walker->new( \%nested_hash ); # only iterating over leaves while ( my ( $k, $v ) = $walker->each ) { print "@{ $k } : $v\n"; } # --> I want to iterate over all nodes. # --> wanted solution for @{ $k }: # ('a') # ('a', 'aa') # ('a', 'aa', 'aaa') # ('a', 'aa', 'aab') # ('a', 'aa', 'aac') # ('a', 'ab') # ('a', 'ab', 'aba') # ('a', 'ab', 'abb') # ('a', 'ab', 'abc') # ('b') # ('b', 'ba') # ('b', 'ba', 'baa') # ('b', 'ba', 'bab') # ('b', 'ba', 'bac') # ('b', 'bb') # ('b', 'bb', 'bba') # ('b', 'bb', 'bbb') # ('b', 'bb', 'bbc')

Thank you very much.

Greetings,

Dirk


In reply to Iterating over nested hash by Dirk80

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.