Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

As poj pointed out, it's rather convenient with base conversion modules. I did a review of some generic ones in a blog article. You can also see shmem's interesting module on his followup thread: Math::Base.

With ntheory and an initial string in $v I'd use something like $v = todigitstring(fromdigits($v,36)+1,36);, possibly adding an extra argument if we want zero padding. Of course we'd only do the fromdigits conversion once if that made sense.

Looking at performance:

             Rate base36__A base36__B base____B base____A ntheory_A ntheory_B
base36__A 0.168/s        --      -42%      -99%      -99%     -100%     -100%
base36__B 0.290/s       73%        --      -99%      -99%     -100%     -100%
base____B  22.1/s    13090%     7522%        --      -16%      -91%      -94%
base____A  26.4/s    15672%     9014%       20%        --      -89%      -93%
ntheory_A   238/s   141957%    81993%      977%      801%        --      -34%
ntheory_B   361/s   215202%   124321%     1532%     1265%       52%        --
Math::Base36 is quite slow, though if you're only doing a few calls (vs. 10k) it won't matter. Math::Base is actually quite fast for being all Perl. All of them will benefit if you can convert to decimal, do a bunch of calculations with that native form, then turn it back. Math::Base gives you the interesting alternative of doing a bunch of calculations directly in the base, only converting when needed.I believe the old Math::Fleximal (written as part of a perlmonks discussion) also does this. An aside that Perl6 for bases up to 36 is easy, intuitive, and all built-in.

#!usr/bin/env perl use warnings; use strict; use Benchmark qw/cmpthese/; use ntheory qw/todigitstring fromdigits/; use Math::Base36 qw/encode_base36 decode_base36/; require "./mathbase.pm"; sub ntheoryA { my $v = shift; $v = todigitstring(fromdigits($v,36)+1,36) for 1..10000; $v; } sub ntheoryB { my $v = shift; my $n = fromdigits($v,36); $v = todigitstring(++$n,36) for 1..10000; $v; } sub base36A { my $v = shift; $v = lc encode_base36(decode_base36($v)+1) for 1..10000; $v; } sub base36B { my $v = shift; my $n = decode_base36($v); $v = lc encode_base36(++$n) for 1..10000; $v; } sub baseA { my $v = shift; my $o = Math::Base->new(36,uc $v,1); $v = lc ++$o for 1..10000; $v; } sub baseB { my $v = shift; my $n = Math::Base->new(36,uc $v,1)->num; $v = lc Math::Base->new(36,++$n) for 1..10000; $v; } { # Check not obviously broken. die "ntheoryA" unless ntheoryA('x000') eq 'x7ps'; die "ntheoryB" unless ntheoryB('x000') eq 'x7ps'; die "base36A" unless base36A('x000') eq 'x7ps'; die "base36B" unless base36A('x000') eq 'x7ps'; die "baseA" unless baseA('x000') eq 'x7ps'; die "baseB" unless baseB('x000') eq 'x7ps'; } cmpthese -5, { ntheory_A => sub { ntheoryA("0000"); }, ntheory_B => sub { ntheoryB("0000"); }, base36__A => sub { base36A("0000"); }, base36__B => sub { base36B("0000"); }, base____A => sub { baseA("0000"); }, base____B => sub { baseB("0000"); }, };


In reply to Re: incrementing mixed letters and numbers by danaj
in thread incrementing mixed letters and numbers by Anonymous Monk

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (8)
    As of 2017-10-23 16:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My fridge is mostly full of:

















      Results (281 votes). Check out past polls.

      Notices?