"Selector, Lev Y" <[EMAIL PROTECTED]> wrote:
>Hello,
>I have integer numbers represented in base 36 (each digit may have one of 36
>values: ('0'..'9','A'..'Z')).
>For example: '5BFHK'.
>I need to increment the number by 1 or by some step (for example, by 25).
>Here is my first take on incrementing by 1:

<snip>

POSIX::strtol() can convert base-36 strings to integers, so all one
needs to do is the opposite conversion.

----------------------------------------------------------------------
use POSIX qw(strtol);
use strict;

sub advance {
    use integer;
    my $instr = shift;
    my $num = 1 + strtol($instr, 36);
    my $outstr = "";
    while ($num > 0) {
        $outstr = ('0'..'9','A'..'Z')[$num % 36] . $outstr;
        $num /= 36;
    }
    return $outstr;
}

for (qw(5BFHK 111Z ZZZZ)) { print "\n$_\n",advance($_),"\n"; }
----------------------------------------------------------------------

The only real hint of fun here is the little-used "use integer", which lets
one say "$num /= 36" instead of "$num = int($num / 36)".

Of course, this probably won't work if your base-36 numbers exceed the size
of a C integer.


--Sean

Reply via email to