As poj says. Heh... didn't know that there's a module for that. This is what I have in my toolbox
my %hash;
my @chars = (0..9,'A'..'Z','a'..'z',map{chr$_}32..47,58..64,91..96
+);
@hash{@chars} = 0..$#chars;
sub encode_base {
my ($base,$num) = @_;
my ($rem,@ret);
while ($num) {
push @ret, $chars[($rem = $num % $base)];
$num -= $rem;
$num /= $base;
}
return join '', reverse @ret;
}
sub decode_base {
my ($base, $str) = @_;
my $num;
$num = $num * $base + $hash{$_} for $str =~ /./g;
$num;
}
which is basically the same, but lets you encode/decode with a base up to 91. So,
my $number = decode_base 36, 1009;
say lc encode_base 36, $number++ for 0..36;
does what you want. Also, these subs can be used to convert from decimal to binary, octal, hex and reverse, using 2, 8 and 16 as base. But I'd use the perl builtins for that ;-)
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.