Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Parse data representing hash

by remiah (Hermit)
on Jun 30, 2014 at 00:39 UTC ( #1091663=note: print w/ replies, xml ) Need Help??


in reply to Parse data representing hash

I was thinkg of another attemt to solve this puzzle.
Making hash notatin text and eval it
And this is nothing better than hdb's one, maybe ...

use strict; use warnings; use Data::Dumper; sub proc{ my($pre, $cnt_cur, $end_brackets)=@_; my $buff; $buff = $pre->{data} ; if ($cnt_cur > $pre->{cnt}){ #next is children $buff .=' => {'; push(@$end_brackets, '}'); }elsif ( $cnt_cur == $pre->{cnt} ){ #next is brothers $buff .= ' => undef ,'; } else { $buff .= ' => undef ,'; for ( 1 .. ($pre->{cnt} - $cnt_cur) ){ #output right bracke +t till that depth $buff .= pop(@$end_brackets) . ","; } } return $buff; } my ($pre, $cnt, $ret, @end_brackets); $ret='{'; while(<DATA>){ $cnt = $_ =~ s/\s{4}//g; $cnt = $cnt || 0; if ( $pre ){ $ret .= proc($pre, $cnt, \@end_brackets); } $pre={cnt => $cnt, data=>$_}; } $ret .= proc($pre, 0, \@end_brackets); $ret .= '}'; print Dumper eval($ret); __DATA__ one two three four three_another four_another five six seven eight nine ten 11 12 13 14 15 16 17 18 19 20 21 22 23 24


Comment on Re: Parse data representing hash
Download Code
Re^2: Parse data representing hash
by peterp (Acolyte) on Jun 30, 2014 at 06:42 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1091663]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-11-01 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (229 votes), past polls