diff options
| author | Abigail <abigail@abigail.be> | 2021-04-28 20:19:16 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-04-28 20:19:16 +0200 |
| commit | 888e31c6215c3c5174ad5e2f88351f2cd51e4c6f (patch) | |
| tree | f5517d3cf683dea2faec47461684de230326ba52 | |
| parent | aec04f00bb9b106567e84b0366024af0c47ca3de (diff) | |
| download | perlweeklychallenge-club-888e31c6215c3c5174ad5e2f88351f2cd51e4c6f.tar.gz perlweeklychallenge-club-888e31c6215c3c5174ad5e2f88351f2cd51e4c6f.tar.bz2 perlweeklychallenge-club-888e31c6215c3c5174ad5e2f88351f2cd51e4c6f.zip | |
Perl and AWK solution should use same algorithm as we use in other languages.
| -rw-r--r-- | challenge-110/abigail/awk/ch-1.awk | 14 | ||||
| -rw-r--r-- | challenge-110/abigail/perl/ch-1.pl | 14 |
2 files changed, 22 insertions, 6 deletions
diff --git a/challenge-110/abigail/awk/ch-1.awk b/challenge-110/abigail/awk/ch-1.awk index 7b5cfbd981..f3b7780f04 100644 --- a/challenge-110/abigail/awk/ch-1.awk +++ b/challenge-110/abigail/awk/ch-1.awk @@ -8,9 +8,13 @@ # Run as: awk -f ch-1.awk < input-file # - {line = $0; - gsub (/ */, "", line)} # Remove spaces +{ + line = $0; + gsub (/ */, "", line) # Remove spaces + sub (/^\+/, "00", line) # Replace leading + with 00 + sub (/^\([0-9]{2}\)/, "0000", line) # Replace leading (NN) with 0000 +} -match (line, /^[0-9]{14}$/) || # nnnn nnnnnnnnnn -match (line, /^\+[0-9]{12}$/) || # +nn nnnnnnnnnn -match (line, /^\([0-9]{2}\)[0-9]{10}$/) {print} # (nn) nnnnnnnnnn +match (line, /^[0-9]{14}$/) { # Match exactly 14 digits + print +} diff --git a/challenge-110/abigail/perl/ch-1.pl b/challenge-110/abigail/perl/ch-1.pl index 4ddafea3f1..c27f7d3ecc 100644 --- a/challenge-110/abigail/perl/ch-1.pl +++ b/challenge-110/abigail/perl/ch-1.pl @@ -28,8 +28,20 @@ use experimental 'lexical_subs'; # can completly ignore any white space in the input. # +# +# To check for valid phone numbers, we will do the following: +# - Remove all whitespace +# - Replace a leading '+' by '00' +# - Replace a leading '(NN)' by '0000' +# +# The number is valid, if and only if we are left with exactly 14 digits. +# + while (<>) { - print if s/\s+//gr =~ /^ (?: \+\d{12} | \(\d{2}\)\d{10} | \d{14} )$/ax + print if s{\s+} {}gr # Remove white space + =~ s{^\+} {00}r # Replace leading + with 00 + =~ s{^\([0-9]{2}\)} {0000}r # Replace leading (NN) with 0000 + =~ /^[0-9]{14}$/ # Check if 14 digits are left } |
