-1

Given a list/array of strings (in particular, UNIX paths), remove the shared part, eg:

  • ./dir/fileA_header.txt
  • ./dir/fileA_footer.txt

I probably will strip the directory before using the function, but strictly speacking this won't change much.

I'd like to know a method to either remove the shared parts (./dir/fileA_) or remove the not-shared part.

Thank you for your help!

Andrea T.
  • 920
  • 4
  • 15
  • 2
    This should be two separate questions, one for Python, and one for Perl. – chepner Jun 10 '19 at 13:29
  • Sounds like this for Python: https://stackoverflow.com/a/46128752/11451509 – Roland Weber Jun 10 '19 at 13:34
  • Possible duplicate of [Python finding the common parts of a string throughout a list and removing it from every item](https://stackoverflow.com/questions/46128615/python-finding-the-common-parts-of-a-string-throughout-a-list-and-removing-it-fr) – Smart Manoj Jun 10 '19 at 13:42

1 Answers1

3

This is a bit of a hack, but if you don't need to support Unicode strings (that is, if all characters have a value below 256), you can use xor to get the length of the longest common prefix of two strings:

my $n = do {
    ($str1 ^ $str2) =~ /^\0*/;
    $+[0]
};

You can apply this operation in a loop to get the common prefix of a list of strings:

use v5.12.0;
use warnings;

sub common_prefix {
    my $prefix = shift;
    for my $str (@_) {
        ($prefix ^ $str) =~ /^\0*/;
        substr($prefix, $+[0]) = '';
    }
    return $prefix;
}

my @paths = qw(
    ./dir/fileA_header.txt
    ./dir/fileA_footer.txt
);

say common_prefix(@paths);

Output: ./dir/fileA_

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 1
    This is also implemented by [String::CommonPrefix](https://metacpan.org/pod/String::CommonPrefix), which should be Unicode-safe but I can't speak to efficiency. – Grinnz Jun 10 '19 at 15:50