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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |