in reply to Parse data representing hash

No need for recursion.

Your HoHoH... has always hashrefs or undef as values.

You need one state var: an @path array holding the refs till the last value so far.

Whenever you parse the indentation you get the index of the next entry in @path and "parent" entry points to the hash you need to extend.

if smaller you need to shorten @path, if bigger you have to extend @path and transform the last undef into a hashref.

I hope you get the idea...

I'm mobile so no chance for tested code, but I'm sure the archive has many examples.

Cheers Rolf

(addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: Parse data representing hash
by peterp (Sexton) on Jun 28, 2014 at 22:21 UTC

    Thank you,

    This information is very useful. If I understand correctly your suggested design is much like what choroba has suggested below, which appears to build the state into an array and pass this to the core DiveRef function.

    Regards,

    Chris

      Similar but not identical from what I see.

      I'd rather keep the values° in @path, choroba keeps the keys.

      Like this I don't need any dive function, the ref of the hash to be extended is already available (or must be autovivified if undef)

      update
      The only complication is that you prefer undef instead of an empty hash for leaves of your HoH tree, which leads to a test condition in edge cases.

      Cheers Rolf

      (addicted to the Perl Programming Language)

      °) or even the refs of the values

        Thanks again, either undef or an empty hashref is fine, either can easily be processed later. I'm currently attempting to implement your suggestion as an alternative to the Data::Diver solution, its pretty late here and my brain is frazzled but I am making progress!