use strict; use warnings; my @dirs; while (my $line = ) { my ($indent, $entry, $isDir) = $line =~ m!^(\s*)([^/]*)(/|\\)?!; next unless defined $entry and length $entry; my $depth = length ($indent); if ($depth < @dirs) { splice @dirs, $depth, $#dirs; } if ($isDir) { push @dirs, $entry; next; } print join ('/', @dirs, $entry); } __DATA__ base/ dir1/ file1.txt a.png dir2/ b.txt q.txt dir3/ a.png f.txt #### base/dir1/file1.txt base/dir1/a.png base/dir1/dir2/b.txt base/dir1/q.txt base/dir3/a.png base/f.txt #### use strict; use warnings; my @dirs; while (my $line = ) { my ($indent, $entry, $isDir) = $line =~ m!^(\s*)([^/]*)(/|\\)?!; next unless defined $entry and length $entry; $#dirs = length $indent; $dirs[-1] = $entry; next if $isDir; print join '/', @dirs; }