"be consistent" PerlMonks

### Re: incrementing mixed letters and numbers

by danaj (Friar)
 on Sep 26, 2017 at 21:11 UTC ( #1200152=note: print w/replies, xml ) Need Help??

in reply to incrementing mixed letters and numbers

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"); },
};

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1200152]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2022-01-27 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2022, my preferred method to securely store passwords is:

Results (70 votes). Check out past polls.

Notices?