diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-11-10 14:13:11 +0000 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-11-10 14:13:11 +0000 |
| commit | 64bfb36a9dfe212ae08df88794c46de50f7ae60c (patch) | |
| tree | 4d6f1dfc2049d8ece613d5592645681a32c6c8d4 | |
| parent | 83c02309afd45cdbfce02a54ee39361fce28c592 (diff) | |
| download | perlweeklychallenge-club-64bfb36a9dfe212ae08df88794c46de50f7ae60c.tar.gz perlweeklychallenge-club-64bfb36a9dfe212ae08df88794c46de50f7ae60c.tar.bz2 perlweeklychallenge-club-64bfb36a9dfe212ae08df88794c46de50f7ae60c.zip | |
Week 347 - Frequent funny formats
| -rw-r--r-- | challenge-347/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-347/peter-campbell-smith/perl/ch-1.pl | 35 | ||||
| -rwxr-xr-x | challenge-347/peter-campbell-smith/perl/ch-2.pl | 39 |
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']; +} |
