use strict; use warnings; use Data::Dumper qw( Dumper ); my %tree; while () { chomp; my $p = \%tree; foreach (split(qr{\s*/\s*}, $_)) { $p = $p->{$_} ||= {}; } } sub re_format { my $data = shift; my $out = []; for my $key (keys %$data) { my $temp = { name => $key}; if (keys %{ $data->{$key} }) { $temp->{children} = re_format($data->{$key}) } push @$out, $temp; } return $out; } print Dumper('First Step' => \%tree, 'Final' => re_format(\%tree)); __DATA__ one foo / bar foo / baz foo / qux / two foo / qux / three foo / qux / four five