Perl Monk, Perl Meditation PerlMonks

### Re: Re: Alpha base-26 to base-10...

by Boots111 (Hermit)
 on Jul 01, 2003 at 05:45 UTC ( #270413=note: print w/replies, xml ) Need Help??

in reply to Re: Alpha base-26 to base-10...
in thread Alpha base-26 to base-10...

John~

I am surprised that no one else has mentioned this (because some have done it in their code), but this is the sort of situation where you want to avoid using exponentiation (be it pow or **). You should notice that the correct power can be built up by repeatedly multiplying by 26 during each pass through the loop. Making your code into:
```# Convert base26 column string to number
# All your Base are belong to us.
my @chars  = split //, \$col;
my \$expn   = 1;
\$col       = 0;

while (@chars) {
my \$char = pop(@chars); # LS char first
\$col += (ord(\$char) -ord('A') +1) * \$expn;
\$expn *= 26;
}

While a really good optimizer *might* do this for you, I doubt that many do, and I am relatively certain that Perl won't since it is so heavily introspective. This is the sort of optimization I always keep my eyes out for cause it can save a lot of work.

Boots
---
Computer science is merely the post-Turing decline of formal systems theory.
--???

Replies are listed 'Best First'.
Re: Re: Re: Alpha base-26 to base-10...
by jmcnamara (Monsignor) on Jul 01, 2003 at 17:05 UTC

this is the sort of situation where you want to avoid using exponentiation (be it pow or **.

I guess you mean that it should be avoided because exponentiation is an expensive operation.

In the code above there would only be at most two iterations through the loop so the effect is small. Nevertheless, it is still easy to avoid it as you have shown.

I wrote that code more than two years ago. If I had to write it again I would probably approach it in the same way. But I would like to have come up with something like sauoq's solution which I think is the nicest solution in this thread (apart form bwana147 golf). ++ in both cases.

--
John.

Create A New User
Node Status?
node history
Node Type: note [id://270413]
help
Chatterbox?
 [perldigious]: but... but... perldigious is the unvirtuous kind of lazy in this case. :-) [perldigious]: Just kidding. Thanks 1nickt, I'll go ahead and do it the right way. An extra set of brackets and a little extra indentation isn't too much to ask. [karlgoethebier]: perldigious: perhaps a block if you are paranoid ;-) [choroba]: but undef %hash and %hash = () both work, too, but the first one keeps the memory allocated, while the latter makes it available for other parts of the program. [choroba]: iirc [perldigious]: karlgoethebier: Well it is a pretty old and complicated (for me) bit of code I wrote (poorly by my current standards), so I'm expecting everything to break when I add the scoping and find out what else is undesireably scope changed. :-) [perldigious]: Ah, thanks choroba, that sort of thing was precisely what I was wondering when I asked.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-07-21 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (335 votes). Check out past polls.