aboutsummaryrefslogtreecommitdiff
path: root/challenge-002
diff options
context:
space:
mode:
authorAilbhe Tweedie <atweedie@protonmail.ch>2019-04-02 18:15:43 +0200
committerAilbhe Tweedie <atweedie@protonmail.ch>2019-04-03 01:08:20 +0200
commit8984c2c8ea170cf84de9f39ea7f43e717ebd2f65 (patch)
treea79386110ae779d1dc0bf84f559c25a03eb26225 /challenge-002
parent176896c928aa3ad1cd7ad98c96f9a010394e1949 (diff)
downloadperlweeklychallenge-club-8984c2c8ea170cf84de9f39ea7f43e717ebd2f65.tar.gz
perlweeklychallenge-club-8984c2c8ea170cf84de9f39ea7f43e717ebd2f65.tar.bz2
perlweeklychallenge-club-8984c2c8ea170cf84de9f39ea7f43e717ebd2f65.zip
002-p5-02: implement command-line switch
Diffstat (limited to 'challenge-002')
-rwxr-xr-xchallenge-002/ailbhe-tweedie/perl5/ch-02.pl26
1 files changed, 21 insertions, 5 deletions
diff --git a/challenge-002/ailbhe-tweedie/perl5/ch-02.pl b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl
index 83c63a5eb2..05826d4efc 100755
--- a/challenge-002/ailbhe-tweedie/perl5/ch-02.pl
+++ b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl
@@ -7,27 +7,43 @@
#
# Prerequisites: cpan install Scalar::Util::Numeric
#
-# Usage: echo {0.999} | ./ch-02.pl
+# Usage:
+# > echo {0.999} | ./ch-02.pl to
+#
+# Test with:
+# > echo {0..99} | ./ch-02.pl to | awk '{print $3}' | ./ch-02.pl from
use strict;
use warnings;
use Scalar::Util::Numeric qw(isint);
use Data::Dump;
+use v5.10;
+
my $BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXY";
-# TODO: add command-line switch to pick between converting TO and FROM
+exit 1 unless @ARGV == 1;
+my $switch = $ARGV[0];
-while (<>) {
+while (<STDIN>) {
chomp;
# TODO: find out how to stream input, instead of saving it to an array first
my @input = split;
for my $i (@input) {
- next unless isint $i;
- my $output = toBase35($i);
+ my $output;
+ if ($switch eq "to") {
+ $output = toBase35($i);
+ } elsif ($switch eq "from") {
+ $output = fromBase35($i);
+ } else {
+ say "Please specify to/from";
+ exit 2;
+ }
+ # `pretty' print output
printf "%-4s\t==>\t%-4s\n", $i, $output;
}
}
+exit 0;
sub toBase35 {
my $input = shift;