in reply to Re^2: Convert delimited string into nested data structure
in thread Convert delimited string into nested data structure
I wrote this up a few years ago.my @stack = { name => 'root', level => 0, children => [] }; foreach my $input ( @inputs ) { my @pieces = split /\s+\/\s+/, $input; my $level = scalar @pieces; my $new_node = { name => pieces[-1], level => $level }; if ( $level == $stack[-1]{level} ) { pop @stack; } elsif ( $level < $stack[-1]{level} ) { pop @stack while ( $level <= $stack[-1]{level} ); } # else top level must be our parent push @stack[-1]{children}, $new_node; push @stack, $new_node }
Phil
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Convert delimited string into nested data structure
by Anonymous Monk on Feb 19, 2007 at 19:16 UTC | |
|
Re^4: Convert delimited string into nested data structure
by Anonymous Monk on Feb 19, 2007 at 19:19 UTC | |
by philcrow (Priest) on Feb 19, 2007 at 19:26 UTC |