in reply to Convert delimited string into nested data structure

Perhaps Recursively walk a hash to get to an element would serve as a good starting point.

Update: I'm sure it could be done, but probably tougher than necessary. So here's a solution from scratch:

use strict; use warnings; my @d; # the data structure we're building. my @p; # current (actually previous) vector while (<DATA>) { chomp; my $r = \@d; my @vc = my @v = split m# / #; my @pc = @p; # bashable copy while ( @vc and @pc and $vc[0] eq $pc[0] ) { shift @vc; shift @pc; $r = $r->[-1]{'children'}; } while ( @vc ) { push @$r, { name => shift @vc }; $r = ( @vc and $r->[-1]{'children'} = [] ); } @p = @v; } use Data::Dumper; print Dumper \@d; __DATA__ one foo / bar foo / baz foo / qux / two foo / qux / three foo / qux / four five

Note, this presumes that the input lines are in proper order, as they are in your example data.

A word spoken in Mind will reach its own level, in the objective world, by its own weight

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.