Thanks for providing your solution to the problem. I found it very interesting particularly as you have taken a different approach to others by building a string then evaluating it into the data structure it represents.

It works absolutely fine for the example data I provided, although my real world keys contain special characters such as { and } therefore I had to adjust to $buff = "'$pre->{data}'";. As a side effect of doing this, I also had to chomp each row.

The only other difference to other solutions I noted when running with my real world data was, my real world data isn't entirely regular i.e. some rows unfortunately have extra spacing (stupid mistake in the code that generated the data) e.g.

one two three

Other solutions unwittingly accounted for this by creating an empty '' key on an intermediate level, although outputting "Use of uninitialized value in hash element" warnings, which I hindered by ensuring the undefined key defaulted to ''. I haven't yet fully understood your code in order to explain why, but under this scenario, your code breaks / generates an invalid data structure.

In order to fix this issue for all solutions, I think I will have to adjust the way depth is calculated, perhaps compare the current row to the previous and check whether there is a bigger or smaller (the complexity is how much smaller) gap as oppose to assuming there will be a change of either +4, 0 or multiples of -4 spaces, or atleast keep track of when the extra spacing occurs and account for it. My real world data isn't irregular enough to make it impossible to decipher which parent level to return to, the extra spacing consistently occurs at particular levels. Perhaps it will be best just to simply programatically clean up the data before processing!

Thanks again


In reply to Re^2: Parse data representing hash by peterp
in thread Parse data representing hash by peterp

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.