### (tye)Re: Base36 numbers: speed and golf

by tye (Sage)
 on Feb 27, 2002 at 22:04 UTC ( #148054=note: print w/replies, xml ) Need Help??

in reply to Base36 numbers: speed and golf

Of course, I can't say whether this is the fastest but it was designed to be fast.

```#!/usr/bin/perl -w
use strict;
my %next;
BEGIN {
@next{'0'..'9','A'..'Z'}= ('1'..'9','A'..'Z','0');
}
sub inc36 {
my \$dig;
while(  not \$dig= \$next{chop(\$_[0])}  ) {
if(  ! length(\$_[0])  ) {
\$dig= '1';
last;
}
}
}
while(  <DATA>  ) {
for(  split " "  ) {
print "\$_ + 1 = ";
inc36( \$_ );
print \$_,\$/;
}
}
__END__
0 1 9 A Y Z 10 19 1Z 9Z AZ ZZ YZZ XYZZY XYZZZ
which outputs:
```0 + 1 = 1
1 + 1 = 2
9 + 1 = A
A + 1 = B
Y + 1 = Z
Z + 1 = 10
10 + 1 = 11
19 + 1 = 1A
1Z + 1 = 20
9Z + 1 = A0
AZ + 1 = B0
ZZ + 1 = 100
YZZ + 1 = Z00
XYZZY + 1 = XYZZZ
XYZZZ + 1 = XZ000
```

- tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Base36 numbers: speed and golf
by jmcnamara (Monsignor) on Feb 28, 2002 at 11:16 UTC

Well done tye. This is an elegant solution and the fastest by a mile.

There is a lot of good code produced here at PerlMonks but I wonder how much of it is actually studied or even looked at. At the time of writing two other solutions that contain bugs have higher reps than this node.

--
John.

Re: (tye)Re: Base36 numbers: speed and golf
by demerphq (Chancellor) on Feb 28, 2002 at 16:31 UTC
Well, this was interesting. I wrote a version of your code that used an array and ord instead of the hash. It was 10% slower in my tests which quite suprised me. I really thought the difference in lookup time between an array and a hash would outway the cost of the ord function, but not so.

Thanks, very educational.

Create A New User
Node Status?
node history
Node Type: note [id://148054]
help
Chatterbox?
 Discipulus yaaawn.. [Corion]: DST does that to everybody... Corion attempts to find who to address to better target his drive-by patches [Corion]: This time it's for the Duktape embedded Javascript engine, which would be nice to have IMO, but doesn't compile on Windows currently [Corion]: I imagine it would be fun to have WWW::Mechanize:: Duktape, but I haven't seen a web browser written in Javascript yet, except the ones distributed with PhantomJs

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2017-03-27 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (319 votes). Check out past polls.