aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Barrett <john@jbrt.org>2019-04-01 22:37:41 +0100
committerJohn Barrett <john@jbrt.org>2019-04-01 22:37:41 +0100
commitf572002dfb9a3a01778dbecbfa44a22c5198a325 (patch)
tree8687732ce6a57e50a140ee43223dfa44baf4bf35
parent0910c851c89e3da165ad33a3454f1f330ec9f1c5 (diff)
downloadperlweeklychallenge-club-f572002dfb9a3a01778dbecbfa44a22c5198a325.tar.gz
perlweeklychallenge-club-f572002dfb9a3a01778dbecbfa44a22c5198a325.tar.bz2
perlweeklychallenge-club-f572002dfb9a3a01778dbecbfa44a22c5198a325.zip
To base35
-rwxr-xr-xchallenge-002/john-barrett/perl5/ch-2.pl30
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-002/john-barrett/perl5/ch-2.pl b/challenge-002/john-barrett/perl5/ch-2.pl
new file mode 100755
index 0000000000..3293529824
--- /dev/null
+++ b/challenge-002/john-barrett/perl5/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+# Usage, e.g.
+# ./ch-2.pl -to-base35 123
+# ./ch-2.pl -from-base35 ABCD
+
+my %args = @ARGV;
+my @charset = ( 0..9, 'A'..'Y' );
+my $base = @charset;
+
+say to_base35( $args{'-to-base35'} ) if $args{'-to-base35'};
+#say to_base35( $args{'-to-base35'} ) if $args{'-to-base35'};
+
+sub to_base35 {
+ my ( $int ) = @_;
+ my $sign = ( $int < 0 ) ? '-' : '';
+ my @digits;
+ $int = abs( $int );
+ do {
+ push @digits, $charset[ $int % $base ];
+ $int = int( $int / $base );
+ say $int;
+ } while ( $int > 0 );
+ $sign . join '', reverse @digits;
+}
+