diff options
| -rw-r--r-- | challenge-259/cheok-yin-fung/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-259/cheok-yin-fung/perl/ch-2.pl | 82 |
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-259/cheok-yin-fung/perl/ch-1.pl b/challenge-259/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..f83f27f7df --- /dev/null +++ b/challenge-259/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,28 @@ +use v5.30.0; +use warnings; +use Date::Simple; +use List::Util qw/any/; + +sub bh { + my $start_date = $_[0]; + my $offset = $_[1]; + my $bank_holidays = $_[2]; + $bank_holidays = [] if !defined($bank_holidays); + my $day = Date::Simple->new($start_date); + my $count = $offset; + while ($count != 0) { + $day = $day->next; + if ($day->day_of_week == 0 || $day->day_of_week == 6 + || + (any {$day == $_} $bank_holidays->@*)) { + } + else { + $count--; + } + } + return sprintf($day); +} + +use Test::More tests=>2; +ok '2018-07-04' eq bh('2018-06-28', 3, ['2018-07-03']); +ok '2018-07-03' eq bh('2018-06-28', 3); diff --git a/challenge-259/cheok-yin-fung/perl/ch-2.pl b/challenge-259/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..c42d8c4d87 --- /dev/null +++ b/challenge-259/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,82 @@ +# The Weekly Challenge 259 +# Task 2 Line Parser +use v5.30.0; +use warnings; + +sub lp { + my $structure; + my $input = $_[0]; + + die "wrong format\n" unless $input =~ /^\{\%\s*(.+)\s*%\}$/; + my $prse = $1; + $prse =~ /^\s*(\w+)/; + my $id = $1; + my @fields; + while ($prse =~ m/(?<=\s{1,255})\w+\=(.+?)(?=\s{1,255}\w+\=(.+?)|$)/g ) { + my $fv_pair = $&; + push @fields, $fv_pair; + } + $structure->{name} = $id; + for my $fv_pair (@fields) { + die "wrong format\n" unless $fv_pair =~ /^(\w+)=(.+)$/; + my $field = $1; + my $value = $2; + if ($value =~ /^\"(.+)\"\s*$/) { + $value = parse_perlstr($1); + } + $structure->{fields}->{$field} = $value; + } + sub parse_perlstr { + my $line = $_[0]; + # BEGIN: source from Text::ParseWords; + $line =~ s/^ + (?: + # double quoted string + (") # $quote + ((?>[^\\"]*(?:\\.[^\\"]*)*))" # $quoted + | # --OR-- + # singe quoted string + (') # $quote + ((?>[^\\']*(?:\\.[^\\']*)*))' # $quoted + | # --OR-- + # unquoted string + ( # $unquoted + (?:\\.|[^\\"'])*? + ) + )//xs; # extended layout + # END: source from Text::ParseWords + return $line; + } + + return $structure; +} + +use Test2::V0; +ok (lp q/{%id field1="value1" field2="value2" field3=42 %}/, + { + name => "id", + fields => { + field1 => "value1", + field2 => "value2", + field3 => 42 + }, + } + ); +ok (lp q/{% youtube title="Title \"quoted\" done" %}/, + { + name => "youtube", + fields => { + title => "Title \"quoted\" done" + }, + } +); +ok (lp q/{% youtube title="Title with escaped backslash \\" %}/, + { + name => "youtube", + fields => { + title => "Title with escaped backslash \"" + }, + } +); + +done_testing(); |
