Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by jmcnamara (Monsignor)
on Jul 01, 2003 at 00:12 UTC ( #270366=note: print w/replies, xml ) Need Help??


in reply to Alpha base-26 to base-10...


Here is some similar code that I wrote a while ago as part of Spreadsheet::WriteExcel::Utility. The code is verbatim although I was tempted to take out some of the parentheses:
# Convert base26 column string to number # All your Base are belong to us. my @chars = split //, $col; my $expn = 0; $col = 0; while (@chars) { my $char = pop(@chars); # LS char first $col += (ord($char) -ord('A') +1) * (26**$expn); $expn++; }

As an aside. Any particular reason for using POSIX::pow()?

And as another aside the POSIX strtol() function will convert base 26 (or bases from 2 to 36) to base 10, although not for this base 26 format.

--
John.

Replies are listed 'Best First'.
Re: Re: Alpha base-26 to base-10...
by Boots111 (Hermit) on Jul 01, 2003 at 05:45 UTC
    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.
    --???

      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.

Re: Re: Alpha base-26 to base-10...
by eduardo (Curate) on Jul 01, 2003 at 00:28 UTC

    Greetings!

    I was using POSIX::pow() because, and am about to hide my head in great shame... I didn't know that perl had the ** operator. I know, I'm a tool.

    Secondly, this is actually to get things out of an excell spreadsheet-ish sort of thing, unfortunately i've discovered that the wonderful human being who created the spreadsheet did things like erase columns, so AE goes right to AG... which makes slicing on numeric offsets *really* a pain in the *******. However, I'm definetly going to check out your module...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://270366]
help
Chatterbox?
[Corion]: erix: Ah. I use where 1 = 0 for queries where I only am interested in the structure ;)
[erix]: 1=0 is as short as TOP :)
[erix]: "code of someone that died" -- kinda nice if your code stops working too
[erix]: hard to implement, hmm
[Corion]: erix: Well, they also seem to have changed the server, or some software, or whatever, and seem to be in the process of changing the DB schema from having the "username" as primary key to something else.
[Corion]: Far too many things being done at once, or maybe only now has it become apparent that nobody knows that piece of software anymore
[marto]: good morning all

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2017-01-23 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (192 votes). Check out past polls.