aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Neleigh <matthew.neleigh@gmail.com>2025-11-11 01:59:31 -0500
committerMatthew Neleigh <matthew.neleigh@gmail.com>2025-11-11 01:59:31 -0500
commit7d8039aca795ef866528c6a8623d60fb80e3dbe2 (patch)
tree621fb418f403dc44ebf457275ba2f7f0aa30f2a0
parentde3c251353838331b46f957ef52be398086de189 (diff)
downloadperlweeklychallenge-club-7d8039aca795ef866528c6a8623d60fb80e3dbe2.tar.gz
perlweeklychallenge-club-7d8039aca795ef866528c6a8623d60fb80e3dbe2.tar.bz2
perlweeklychallenge-club-7d8039aca795ef866528c6a8623d60fb80e3dbe2.zip
new file: challenge-347/mattneleigh/perl/ch-1.pl
new file: challenge-347/mattneleigh/perl/ch-2.pl
-rwxr-xr-xchallenge-347/mattneleigh/perl/ch-1.pl71
-rwxr-xr-xchallenge-347/mattneleigh/perl/ch-2.pl84
2 files changed, 155 insertions, 0 deletions
diff --git a/challenge-347/mattneleigh/perl/ch-1.pl b/challenge-347/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..cab99d6036
--- /dev/null
+++ b/challenge-347/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @dates = (
+ "1st Jan 2025",
+ "22nd Feb 2025",
+ "15th Apr 2025",
+ "23rd Oct 2025",
+ "31st Dec 2025"
+);
+
+print("\n");
+foreach my $date (@dates){
+ printf(
+ "Input: \$str = \"%s\"\nOutput: \"%s\"\n\n",
+ $date,
+ date_to_iso_format($date)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a date in the form of an ordinal day, capitalized three-letter
+# abbreviation of the month, and a four-digit year (e.g. "10th Nov 2025")
+# convert it to ISO 8601 date format, i.e. "2025-11-10"
+# Takes one argument:
+# * The date string to convert (see above for example)
+# Returns:
+# * The supplied date in ISO 8601 format (see above for example)
+################################################################################
+sub date_to_iso_format{
+ my @day_month_year = split(" ", shift());
+
+ my %months = (
+ Jan => "01", Feb => "02", Mar => "03", Apr => "04",
+ May => "05", Jun => "06", Jul => "06", Aug => "08",
+ Sep => "09", Oct => "10", Nov => "11", Dec => "12"
+ );
+
+ # Strip the ordinal suffix from the day
+ $day_month_year[0] =~ s/\D//g;
+
+ return(
+ $day_month_year[2]
+ .
+ "-"
+ .
+ $months{$day_month_year[1]}
+ .
+ "-"
+ .
+ sprintf("%02d", $day_month_year[0])
+ );
+
+}
+
+
+
diff --git a/challenge-347/mattneleigh/perl/ch-2.pl b/challenge-347/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..e42bc78ff5
--- /dev/null
+++ b/challenge-347/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @phone_numbers = (
+ "1-23-45-6",
+ "1234",
+ "12 345-6789",
+ "123 4567",
+ "123 456-78"
+);
+
+print("\n");
+foreach my $phone_number (@phone_numbers){
+ printf(
+ "Input: \$phone = \"%s\"\nOutput: \"%s\"\n\n",
+ $phone_number,
+ reformat_telephone_number($phone_number)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a telephone number consisting only of digits, spaces, or dashes,
+# reformat it into dash-separated blocks according to the following rules:
+# 1) All digits will be grouped into blocks of three (3) except...
+# 2) The final four (4) or fewer digits, if any, will be...
+# 2a) ...presented as a single block of digits if fewer than four digits
+# remain
+# 2b) ...presented as two blocks of two digits if four digits remain
+# Takes one argument:
+# * The phone number to process (e.g. "123 4567")
+# Returns:
+# * The phone number reformatted as described above (e.g. "123-45-67")
+################################################################################
+sub reformat_telephone_number{
+ # Grab only the digits from the input
+ my @digits = grep(/\d/, split("", shift()));
+
+ my $reformatted = "";
+
+ # Create blocks of three digits, leaving only the
+ # last four or fewer digits for later processing
+ while(scalar(@digits) > 4){
+ $reformatted .= "-"
+ if(length($reformatted));
+ $reformatted .= join("", splice(@digits, 0, 3));
+ }
+
+ # Handle remaining digits, if necessary
+ if(scalar(@digits)){
+ if(scalar(@digits) == 4){
+ # 4 digits
+ for(0 .. 1){
+ $reformatted .= "-"
+ if(length($reformatted));
+ $reformatted .= join("", splice(@digits, 0, 2));
+ }
+ } else{
+ # 1-3 digits
+ $reformatted .= "-"
+ if(length($reformatted));
+ $reformatted .= join("", splice(@digits, 0));
+ }
+ }
+
+ return($reformatted);
+
+}
+
+
+