diff options
| author | Jaime <42359730+bracteatus@users.noreply.github.com> | 2019-06-14 08:52:16 -0600 |
|---|---|---|
| committer | Jaime <42359730+bracteatus@users.noreply.github.com> | 2019-06-14 08:52:16 -0600 |
| commit | 31637ba5aad2a8ad7767c7ebeed9e760de7b7b8a (patch) | |
| tree | 84368889ae372a6562066b39f3ab80903a55abcb | |
| parent | b3aeb665d757158d9c558169b9d791537cb44f96 (diff) | |
| download | perlweeklychallenge-club-31637ba5aad2a8ad7767c7ebeed9e760de7b7b8a.tar.gz perlweeklychallenge-club-31637ba5aad2a8ad7767c7ebeed9e760de7b7b8a.tar.bz2 perlweeklychallenge-club-31637ba5aad2a8ad7767c7ebeed9e760de7b7b8a.zip | |
Update ch-2.pl
Use REGEX extraction to find intersections of paths.
| -rw-r--r-- | challenge-012/jaime/perl5/ch-2.pl | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/challenge-012/jaime/perl5/ch-2.pl b/challenge-012/jaime/perl5/ch-2.pl index 7a4262c7f6..15c1312e46 100644 --- a/challenge-012/jaime/perl5/ch-2.pl +++ b/challenge-012/jaime/perl5/ch-2.pl @@ -15,3 +15,23 @@ # # and the path separator is `/`. Your script should return `/a/b` as common # directory path. + +my $delim = shift; # delimit a path, for example `/`. + +my $pelim = ":"; # delimit paths, for example unix uses `:`. +my @paths = split /$pelim/, shift; # list of paths, for example `/bin:/sbin`. + +# at best, the first path is completely common within all paths +my $common = @paths[0]; + +PATH: for my $path (@paths) { + next PATH if $path =~ /^$common/; # $common at the start of $path. + while (($common) = ($common =~ /^(.*)$delim/)) { + last if $path =~ /^$common/; # otherwise find intersection of $common and $path. + } + last PATH unless $common; # stop if a $path is completely unique. +} + +($common) = (@paths[0] =~ /^($delim)/) unless $common; # if given, root is always common + +print "$common\n"; |
