in reply to Finding nested directories from a string list.

I don't understand why you don't want C:\\c and c:\\car in your result list. From your explanation, I think your nested hash approach is an elegant solution. I have another solution that relies on the fact that a subdirectory will always sort at a later place than its parent:

use strict; my @paths = ( 'C:\\foo\\bar\\baz', 'C:\\foo\\bar', 'C:\\car', 'C:\\c' ); my $last = ""; my @nested; for (sort @paths) { if (not @nested) { push @nested, $_; } elsif (/^\Q$nested[-1]\E\\/i) { $nested[-1] = $_; } else { push @nested, $_; }; }; print "$_\n" for @nested;

On Windows, we have a case-insensitive file system, so we want to compare case-insensitive. This will possibly fail if you have umlauts in your directory names that should collate to the same character.

Replies are listed 'Best First'.
Re^2: Finding nested directories from a string list.
by superfrink (Curate) on Feb 24, 2006 at 20:56 UTC
    I don't understand why you don't want C:\\c and c:\\car in your result list.

    Actually I have a list of directories with property X. I need to filter out all instances of nested X-ness. ie any directory with property X must not appear under another directory with property X. In such a case I have to remove the property from the inner directory. It's an application thing. :)