From db15b50ebbaeb24ebe4b4554d26d6175b52966f5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 May 2021 18:16:13 +0200 Subject: Use same algorithm as other languages --- challenge-112/abigail/perl/ch-1.pl | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/challenge-112/abigail/perl/ch-1.pl b/challenge-112/abigail/perl/ch-1.pl index b68a966191..5d7a0157d5 100644 --- a/challenge-112/abigail/perl/ch-1.pl +++ b/challenge-112/abigail/perl/ch-1.pl @@ -19,25 +19,17 @@ use experimental 'lexical_subs'; while (<>) { chomp; - - # Remove duplicate slashes - s !/\K/+!!g; - - # Add a trailing slash; this makes it easier to deal - # with the cases below. - $_ .= "/"; - - # Remove single period - s !/\.(?=/)!!g; - - # Remove double period - 1 while s !/[^/]+/\.\.(?=/)!!; - - # Remove any leading /../ - 1 while s !^/\.\./!/!; - - # Remove trailing slashes - s !/+$!!; - - say $_ || '/'; + my @parts = split /\/+/; + my @parts2; + + foreach my $part (@parts) { + next if $part eq "." || $part eq ""; + if ($part eq "..") { + pop @parts2; + next; + } + push @parts2 => $part; + } + + say "/" . join "/" => @parts2; } -- cgit