The OP requested not to include helloworld/ since there were subdirectories. This addition to your script works but will not be efficient for a large file with the nested searches.

use strict; use warnings; use File::Basename qw( fileparse ); my %seen; while (<DATA>) { (undef, my $path) = fileparse( $_ ); print "$path\n" unless $seen{$path}++; } print "="x70,"\n"; foreach my $key (keys %seen){ print "$key\n" if 1 == scalar grep { /\Q$key/ } keys %seen; } __DATA__ testing123/ foobar/ helloworld/ helloworld/r1/ helloworld/r1/helloworld-5-0.noarch.rpm helloworld/r1/testfile23.txt helloworld/r1/tomcat-7.0.27.rpm helloworld/r2/ helloworld/r2/helloworld-2-0.noarch.rpm helloworld/r2/testfile12.txt helloworld/r2/tomcat-5.0.52.rpm hellotest/

For large files sorting and a pass to remove the current element if the next element matches is needed. Or, assuming that the original file is sorted just check each element against the next one.

use strict; use warnings; use File::Basename qw( fileparse ); my %seen; my @directories; while (<DATA>) { (undef, my $path) = fileparse( $_ ); #push @directories, $path; ## update, I indended to put the push inside the unless block. ## The original works but not exactly as I expected since it puts ## duplicates in the array. #print "$path\n" unless $seen{$path}++; unless ($seen{$path}++) { print "$path\n"; push @directories, $path; } } print "="x70,"\n"; foreach my $index (0..$#directories-1){ my $current = $directories[$index]; print "$current\n" if not $directories[$index+1] =~ /\Q$current/ ; } ### assuming the last one is always terminal print $directories[$#directories], "\n"; __DATA__ testing123/ foobar/ helloworld/ helloworld/r1/ helloworld/r1/helloworld-5-0.noarch.rpm helloworld/r1/testfile23.txt helloworld/r1/tomcat-7.0.27.rpm helloworld/r2/ helloworld/r2/helloworld-2-0.noarch.rpm helloworld/r2/testfile12.txt helloworld/r2/tomcat-5.0.52.rpm hellotest/

Update: This seems to work on a single pass through the file.

use strict; use warnings; use File::Basename qw( fileparse ); my $first=''; my $second=''; while (<DATA>) { $first = $second; (undef, $second) = fileparse( $_ ); print "$second\n" if eof DATA; next unless $first; unless ( $second =~ /\Q$first/) { print "$first\n"; } } __DATA__ testing123/ foobar/ helloworld/ helloworld/r1/ helloworld/r1/helloworld-5-0.noarch.rpm helloworld/r1/testfile23.txt helloworld/r1/tomcat-7.0.27.rpm helloworld/r2/ helloworld/r2/helloworld-2-0.noarch.rpm helloworld/r2/testfile12.txt helloworld/r2/tomcat-5.0.52.rpm hellotest/test.txt hellotest/

In reply to Re^2: Finding deepest directories in a tree structure represented in a flatfile? by Lotus1
in thread Finding deepest directories in a tree structure represented in a flatfile? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.