aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-11-10 18:59:44 +0000
committerGitHub <noreply@github.com>2025-11-10 18:59:44 +0000
commitb1409ac9501f0f471d5b0339b97ec40b49fe75e5 (patch)
tree16ac46284ed53379f981614ab9e3f2ffe92608da
parent51aa911461851ad69a2d39ce1a16c6d6623fe86a (diff)
parent64bfb36a9dfe212ae08df88794c46de50f7ae60c (diff)
downloadperlweeklychallenge-club-b1409ac9501f0f471d5b0339b97ec40b49fe75e5.tar.gz
perlweeklychallenge-club-b1409ac9501f0f471d5b0339b97ec40b49fe75e5.tar.bz2
perlweeklychallenge-club-b1409ac9501f0f471d5b0339b97ec40b49fe75e5.zip
Merge pull request #13004 from pjcs00/wk347
Week 347 - Frequent funny formats
-rw-r--r--challenge-347/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-347/peter-campbell-smith/perl/ch-1.pl35
-rwxr-xr-xchallenge-347/peter-campbell-smith/perl/ch-2.pl39
3 files changed, 75 insertions, 0 deletions
diff --git a/challenge-347/peter-campbell-smith/blog.txt b/challenge-347/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..e4bc0ae499
--- /dev/null
+++ b/challenge-347/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/347
diff --git a/challenge-347/peter-campbell-smith/perl/ch-1.pl b/challenge-347/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..91dd6167ad
--- /dev/null
+++ b/challenge-347/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-11-10
+use utf8; # Week 347 - task 1 - Format date
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+format_date('15th Jan 2025');
+format_date('22nd Feb 2025');
+format_date('1st Jan 1900');
+format_date('31st Dec 2100');
+format_date('10th Nov 2025');
+
+sub format_date {
+
+ my ($string, $output, %mo, $d, $m, $y, $i);
+
+ # initialise
+ $string = shift;
+ %mo = (Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6,
+ Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12);
+
+ # do it
+ if (($d, $m, $y) = $string =~ m|(\d{1,2}).+?([a-z]{3}).+?(\d{4})|i) {
+ $output = sprintf('%04d-%02d-%02d', $y, $mo{$m}, $d);
+ } else {
+ $output = 'illegal format';
+ }
+
+ say qq[\nInput: '$string'];
+ say qq[Output: $output];
+}
diff --git a/challenge-347/peter-campbell-smith/perl/ch-2.pl b/challenge-347/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..680170bd76
--- /dev/null
+++ b/challenge-347/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-11-10
+use utf8; # Week 347 - task 2 - Format phone number
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+format_phone_number('1-23-45-6');
+format_phone_number('1234');
+format_phone_number('12 345-6789');
+format_phone_number('123 4567');
+format_phone_number('123 456-78');
+format_phone_number('02072221212');
+
+sub format_phone_number {
+
+ my ($phone, $output, $length);
+
+ # initialise
+ $phone = shift;
+ say qq[\nInput: '$phone'];
+ $phone =~ s|[^[0-9]||g;
+
+ # groups of 3
+ while (length($phone) > 4) {
+ $output .= substr($phone, 0, 3) . '-';
+ $phone = substr($phone, 3);
+ }
+
+ # and the rest
+ $output .= length($phone) == 4
+ ? substr($phone, 0, 2) . '-' . substr($phone, 2, 2)
+ : $phone;
+
+ say qq[Output: '$output'];
+}