From 8984c2c8ea170cf84de9f39ea7f43e717ebd2f65 Mon Sep 17 00:00:00 2001 From: Ailbhe Tweedie Date: Tue, 2 Apr 2019 18:15:43 +0200 Subject: 002-p5-02: implement command-line switch --- challenge-002/ailbhe-tweedie/perl5/ch-02.pl | 26 +++++++++++++++++++++----- 1 file 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 () { 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; -- cgit