Skip to content

Instantly share code, notes, and snippets.

@paulgregg
Created October 26, 2014 13:15
Show Gist options
  • Save paulgregg/f7500dc77c23675bfe7b to your computer and use it in GitHub Desktop.
Save paulgregg/f7500dc77c23675bfe7b to your computer and use it in GitHub Desktop.
Extract unique subdirectories from a file, ignoring parent dirs
#!/usr/bin/perl
# pgregg@pgregg.com
use Data::Dumper;
my $d={};
while(<STDIN>) {
chomp;
next if(/^$/);
@bits = split(/\//, $_);
$last = pop(@bits);
$dr = $d;
for my $bit (@bits) {
next if ($bit eq "");
if (exists $dr->{$bit}) {
if (ref $dr->{$bit} ne HASH) {
#print "deleting $bit\n";
delete $dr->{$bit};
#print "reinit $bit\n";
$dr->{$bit} = {};
} else {
#print "ignoring $bit\n";
}
} else {
#print "init $bit\n";
$dr->{$bit} = {};
}
#print "moving ptr\n";
$dr = $dr->{$bit};
}
if (!exists $dr->{$last}) {
$dr->{$last} = 1;
}
}
print Dumper($d);
sub walkhashref {
my $arg = shift;
my $prefix = shift;
my $line;
foreach $k (keys %{$arg}) {
$line = $prefix . "/$k";
if (ref $arg->{$k} eq HASH) {
walkhashref($arg->{$k}, $line);
} else {
print $line . "\n";
}
}
}
walkhashref($d, "");
@paulgregg
Copy link
Author

Sample Input:
/a
/a/b/
/a/b/c
/g/
/g/h
/x/y/z/foo
/x/y/bar
/x/y
/a

@paulgregg
Copy link
Author

Output:


$ ./dirunique2.pl < dirs.txt
$VAR1 = {
'a' => {
'b' => {
'c' => 1
}
},
'g' => {
'h' => 1
},
'x' => {
'y' => {
'bar' => 1,
'z' => {
'foo' => 1
}
}
}
};
/a/b/c
/g/h
/x/y/bar
/x/y/z/foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment