Well, you could convert the base 36 number to Perl's representation, add one, and then convert it back to base 36...
{ my @to_b36 = (0 .. 9, 'A' .. 'Z'); my %to_num; @to_num{@to_b36} = 0 .. 35; sub b36_to_num { my $n = shift; my ($i, $s) = (0, 0); $s += $to_num{chop $n} * 36**$i++ while length $n; return $s; } sub num_to_b36 { use integer; # so that /= 36 is easy my $n = shift; my $s = ""; do { $s = $to_b36[$n % 36] . $s, $n /= 36 } while $n; return $s; } sub inc_b36 { num_to_b36(&b36_to_num + 1) } }
That's the complete solution. You don't need to do all that work, though. Here's a much simpler regex solution:
{ # in the same block as the previous code sub rx_inc_b36 { my $n = shift; $n =~ s/([^Z])(Z*)$/$to_b36[$to_num{$1}+1] . 0 x length $2/e or $n = 1 . 0 x length $n; return $n; } }
You could also use my reversed regex approach:
{ # ditto sub rx_inc_b36 { my $n = reverse shift; $n =~ s/^(Z*)([^Z])/(0 x length $1) . $to_b36[$to_num{$2}+1]/e or $n = (0 x length $n) . 1; return scalar reverse $n; } }
And here is my golf code, which registers 94 characters:
sub GOLF { local$_=pop;@_{@_=( 0..9,A..Z)}=0..35;s /([^Z])(Z*)$/$_[$_{ $1}+1].$-x length$2 /e?$_:1 .$-x y!!!c }

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;


In reply to Re: Base36 numbers: speed and golf by japhy
in thread Base36 numbers: speed and golf by nop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.