aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-25 19:28:12 +0100
committerAbigail <abigail@abigail.be>2021-01-25 19:28:12 +0100
commit9aeed56c942517679d2e97deea955c6f72abd70a (patch)
treea00fd25dc26a9c29c2f3647a28f0a05d43f7c5dc
parent4d628f1e0675144dddb903665fd94040fa49b832 (diff)
downloadperlweeklychallenge-club-9aeed56c942517679d2e97deea955c6f72abd70a.tar.gz
perlweeklychallenge-club-9aeed56c942517679d2e97deea955c6f72abd70a.tar.bz2
perlweeklychallenge-club-9aeed56c942517679d2e97deea955c6f72abd70a.zip
Only use one one hash to map digits.
-rw-r--r--challenge-002/abigail/perl/ch-2.pl18
1 files changed, 10 insertions, 8 deletions
diff --git a/challenge-002/abigail/perl/ch-2.pl b/challenge-002/abigail/perl/ch-2.pl
index d02cf2a999..a9b9c006ba 100644
--- a/challenge-002/abigail/perl/ch-2.pl
+++ b/challenge-002/abigail/perl/ch-2.pl
@@ -25,17 +25,19 @@ die "Need exactly one of -t or -f" unless $to_base xor $from_base;
my $BASE = 35;
-my @digits = (0 .. 9, 'A' .. 'Y');
-my %to;
-@to {keys @digits} = @digits;
-my %from = reverse %to;
-
+my %digits;
+$digits {$_} = $_ for 0 .. 9;
+foreach my $n (10 .. $BASE - 1) {
+ my $ch = chr (65 + $n - 10);
+ $digits {$ch} = $n;
+ $digits {$n} = $ch;
+}
sub to_base ($number) {
my $out = "";
while ($number) {
- $out = $to {$number % $BASE} . $out;
- $number = int ($number / $BASE);
+ $out = $digits {$number % $BASE} . $out;
+ $number = int ($number / $BASE);
}
$out || "0";
}
@@ -45,7 +47,7 @@ sub from_base ($number) {
while (length $number) {
my $digit = substr $number, 0, 1, "";
$out *= $BASE;
- $out += $from {$digit};
+ $out += $digits {$digit};
}
$out;
}