Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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 "./"; 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

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

  • 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 or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-08-15 04:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found