my @tree_full_paths; my @path; for ( @tree ) { /^( *)(.+)/ or next; $#path = length $1; $path[-1] = $2; push @tree_full_paths, join '/', @path; }