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