in reply to Re: Convert delimited string into nested data structure
in thread Convert delimited string into nested data structure
As such I keep tearing down my past efforts -- but just so you know I am working on this instead of just waiting for others to do my work for me ...
Which produces the following output:sub recurse { my ($ref, $car, @cdr) = @_; if ($prev_car eq $car) { my $hash = { name => $car, children => [] }; push @$ref, $hash; return unless @cdr; $prev_car = $car; recurse($hash->{children}, @cdr); } push @$ref, { name => $car, children => [] }; return unless @cdr; $prev_car = $car; recurse($ref, @cdr); }
$VAR1 = [ { 'name' => 'one', 'children' => [] }, { 'name' => 'foo', 'children' => [] }, { 'name' => 'bar', 'children' => [] }, { 'name' => 'foo', 'children' => [ { 'name' => 'baz', 'children' => [] } ] }, { 'name' => 'foo', 'children' => [] }, { 'name' => 'baz', 'children' => [] }, { 'name' => 'foo', 'children' => [ { 'name' => 'qux', 'children' => [] }, { 'name' => 'two', 'children' => [] } ] }, { 'name' => 'foo', 'children' => [] }, { 'name' => 'qux', 'children' => [] }, { 'name' => 'two', 'children' => [] }, { 'name' => 'foo', 'children' => [] }, { 'name' => 'qux', 'children' => [] }, { 'name' => 'three', 'children' => [] }, { 'name' => 'foo', 'children' => [] }, { 'name' => 'qux', 'children' => [] }, { 'name' => 'four', 'children' => [] }, { 'name' => 'five', 'children' => [] } ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Convert delimited string into nested data structure
by educated_foo (Vicar) on Feb 19, 2007 at 19:35 UTC | |
by merlyn (Sage) on Feb 19, 2007 at 20:21 UTC | |
| |
|