aboutsummaryrefslogtreecommitdiff
path: root/challenge-012/maxim-nechaev/perl5/ch-2.pl
blob: a400b1ecc016a4cf05e47ddbd28ff27b1947da55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/perl -w
use strict;
use feature 'say';
use List::Util qw/all/;

sub common_root {
    my ($sep, @paths) = @_;

    my $root = '';
    foreach my $part ( split $sep, $paths[0] ) {
	next unless $part;
	last unless all { m/^${root}${sep}${part}($sep|$)/ } @paths;
	$root .= $sep.$part;
    }
    return $root;
}

say common_root '/', ('/a/b/c/d', '/a/b/cd', '/a/b/cc', '/a/b/c/d/e');