The stupid question is the question not asked PerlMonks

### (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 \$add= ""; my \$dig; while( not \$dig= \$next{chop(\$_[0])} ) { \$add .= \$dig; if( ! length(\$_[0]) ) { \$dig= '1'; last; } } \$_[0] .= \$dig . \$add; } 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?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2018-06-20 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (116 votes). Check out past polls.

Notices?