aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2021-04-27 22:38:22 +1000
committerSimon Green <mail@simon.green>2021-04-27 22:38:22 +1000
commitb62cd56fbb11e6073324daf59961dfa56279ee07 (patch)
tree41a1284ff7a1a23693660e0aac37766b2669bc12
parentbea1f191fc371a54d6e51a0a8aa541cc716c74d1 (diff)
downloadperlweeklychallenge-club-b62cd56fbb11e6073324daf59961dfa56279ee07.tar.gz
perlweeklychallenge-club-b62cd56fbb11e6073324daf59961dfa56279ee07.tar.bz2
perlweeklychallenge-club-b62cd56fbb11e6073324daf59961dfa56279ee07.zip
sgreen solution to challenge 110
-rw-r--r--challenge-110/sgreen/README.md4
-rw-r--r--challenge-110/sgreen/blog.txt1
-rwxr-xr-xchallenge-110/sgreen/perl/ch-1.pl23
-rwxr-xr-xchallenge-110/sgreen/perl/ch-2.pl33
4 files changed, 59 insertions, 2 deletions
diff --git a/challenge-110/sgreen/README.md b/challenge-110/sgreen/README.md
index 94ad2ed1db..53f7bdfaba 100644
--- a/challenge-110/sgreen/README.md
+++ b/challenge-110/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 109
+# The Weekly Challenge 110
-Solution by Simon Green. [Blog]()
+Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-110-22ao)
diff --git a/challenge-110/sgreen/blog.txt b/challenge-110/sgreen/blog.txt
new file mode 100644
index 0000000000..2dd0651112
--- /dev/null
+++ b/challenge-110/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-110-22ao
diff --git a/challenge-110/sgreen/perl/ch-1.pl b/challenge-110/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..5c420464ee
--- /dev/null
+++ b/challenge-110/sgreen/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub main {
+ my $filename = shift;
+ die "File does not exist or is not readable\n" unless -f $filename && -r $filename;
+
+ # Open the file
+ open( my $fh, '<', $filename ) or die "Cannot open $filename: $!";
+
+ while ( my $line = <$fh> ) {
+ # Remove the trailing cr/nl characters
+ chomp $line;
+
+ # Print the line if it is the correct format
+ say $line if $line =~ /^(?:\+[0-9]{2}|\([0-9]{2}\)|[0-9]{4}) [0-9]{10}$/;
+ }
+}
+
+main(@ARGV);
diff --git a/challenge-110/sgreen/perl/ch-2.pl b/challenge-110/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..a5354cc5f9
--- /dev/null
+++ b/challenge-110/sgreen/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+use List::Util 'max';
+
+sub main {
+ my $filename = shift;
+ die "File does not exist or is not readable\n" unless -f $filename && -r $filename;
+
+ # Open the file
+ open( my $fh, '<', $filename ) or die "Cannot open $filename: $!";
+
+ # Read the input into an array
+ my @lines = ();
+ while ( my $line = <$fh> ) {
+ # Remove the trailing cr/nl characters
+ chomp $line;
+ push @lines, [ split /,/, $line ];
+ }
+
+ # Get the maximum number of values in the rows
+ my $max = max( map { scalar(@$_) } @lines );
+
+ # Print each column as a row
+ for my $col ( 0 .. $max - 1 ) {
+ say join ',', map { $_->[$col] // '' } @lines;
+ }
+}
+
+main(@ARGV);