#!/usr/bin/perl
use strict;
use warnings;
# Base 24 question from https://twitter.com/pwnallthethings/status/13
+43590455511023621
my @digits = qw/B C D F G H J K M P Q R T V W X Y 2 3 4 6 7 8 9/;
my %digit_value;
my $string = 'FCKGW-RHQQ2-YXRKT-8TG6W-2B7Q8';
# Convert a base 24 number to decimal. This is a group of five
# numbers, like a Windows key.
{
my $v = 0;
foreach my $d ( @digits ) {
$digit_value{ $d } = $v++;
}
my @units = split ( /-/, $string );
foreach my $u ( @units ) {
my $value = decode ( $u );
print "$u -> $value\n";
}
}
sub decode
{
my ( $str ) = @_;
my $val = 0;
my @chars = split ( //, $str );
foreach my $c ( @chars ) {
$val *= 24;
$val += $digit_value{ $c };
}
return $val;
}
There's no exit required because the closing brace indicates "That's the end!" to Perl. I have seen code where the mainline is right up against the left margin -- I never code like that, because it obscures what's going on -- it's the same level as the use statements, so I don't know where the declarations end and the executable code starts.
And I don't put an exit anywhere, because that would indicate I was bailing out early, instead of allowing the syntax to naturally indicate where the end of the procedure is. To be clear, I'm *not* saying your way is wrong -- it's just not what I prefer. I like my way because it's clear and legible. For me, white space is my friend.
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.
|