#! perl -slw use strict; use Data::Dumper; sub x { my $aref = shift; my( $name, @kids ) = @_; unless( @$aref and $aref->[ -1 ]{ name } eq $name ) { push @{ $aref }, { name => $name }; } if( @kids ) { if( not exists $aref->[ -1 ]{ children } ) { $aref->[ -1 ]{ children } = []; } x( $aref->[ -1 ]{ children }, @kids ); } } my @data; chomp() and x( \@data, split ' / ', $_ ) while ; print Dumper \@data; __DATA__ one foo / bar foo / baz foo / qux / two foo / qux / three foo / qux / four five