aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-03 17:12:46 +0100
committerGitHub <noreply@github.com>2021-05-03 17:12:46 +0100
commitf9b9d2a2ef371ebeb3b975247a2d58308f4db599 (patch)
tree8361e1c0f10a24c2402388e54aa707a4eb5838d6
parent0381a39b17ccd040302474f25d3c1cbbef703327 (diff)
parent8344bcc5d9c8967c4f93438f53d4fbcf4453ad77 (diff)
downloadperlweeklychallenge-club-f9b9d2a2ef371ebeb3b975247a2d58308f4db599.tar.gz
perlweeklychallenge-club-f9b9d2a2ef371ebeb3b975247a2d58308f4db599.tar.bz2
perlweeklychallenge-club-f9b9d2a2ef371ebeb3b975247a2d58308f4db599.zip
Merge pull request #4001 from choroba/ech110
Solve 110: Valid Phone Numbers & Transpose File by E. Choroba
-rwxr-xr-xchallenge-110/e-choroba/perl5/ch-1.pl29
-rwxr-xr-xchallenge-110/e-choroba/perl5/ch-2.pl39
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-110/e-choroba/perl5/ch-1.pl b/challenge-110/e-choroba/perl5/ch-1.pl
new file mode 100755
index 0000000000..6a36f86903
--- /dev/null
+++ b/challenge-110/e-choroba/perl5/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+my $VALID = qr/^\s* (?: \([0-9]{2}\)
+ | \+[0-9]{2}
+ | [0-9]{4}
+ ) \s+
+ [0-9]{10} $/x;
+
+sub valid_phone_numbers {
+ my @valid = grep /$VALID/, @_;
+ chomp @valid;
+ return \@valid
+}
+
+use Test::More tests => 1;
+
+is_deeply valid_phone_numbers(<DATA>),
+ ['0044 1148820341',
+ ' +44 1148820341',
+ '(44) 1148820341'];
+
+__DATA__
+0044 1148820341
+ +44 1148820341
+ 44-11-4882-0341
+(44) 1148820341
+ 00 1148820341
diff --git a/challenge-110/e-choroba/perl5/ch-2.pl b/challenge-110/e-choroba/perl5/ch-2.pl
new file mode 100755
index 0000000000..08bdf011aa
--- /dev/null
+++ b/challenge-110/e-choroba/perl5/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+sub transpose_file {
+ my ($input, $output) = @_;
+ open my $in, '<', $input or die $!;
+ open my $out, '>', $output or die $!;
+
+ my $maybe_comma = "";
+ my @tr;
+ while (<$in>) {
+ chomp;
+ my @cells = split /,/;
+ $tr[$_] .= "$maybe_comma$cells[$_]" for 0 .. $#cells;
+ $maybe_comma = ',';
+ }
+ say {$out} $_ for @tr;
+}
+
+use Test::More tests => 1;
+
+my $file = << '__INPUT__';
+name,age,sex
+Mohammad,45,m
+Joe,20,m
+Julie,35,f
+Cristina,10,f
+__INPUT__
+
+my $expected = << '__EXPECTED__';
+name,Mohammad,Joe,Julie,Cristina
+age,45,20,35,10
+sex,m,m,f,f
+__EXPECTED__
+
+transpose_file(\$file, \ my $outfile);
+is $outfile, $expected;