<?xml version="1.0" encoding="windows-1252"?>
<node id="270352" title="Alpha base-26 to base-10..." created="2003-06-30 18:40:37" updated="2005-08-08 07:17:33">
<type id="115">
perlquestion</type>
<author id="7056">
eduardo</author>
<data>
<field name="doctext">
&lt;p&gt;Greetings!&lt;/p&gt;
&lt;p&gt;So, I have a problem at my current contract that proved to be surprisingly annoying, and my solution looks remarkably inelegant to me.  So, I thought to myself: "I'll ask the perlmonks, surely someone will give me a 8 character regexp that solves my problem elegantly!" :)&lt;/p&gt;
&lt;p&gt;I have a series of "alphabet based base-26" numbers... what do I mean by this?  Well how about some examples so you get the idea...&lt;/p&gt;
&lt;p&gt;
&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;Base-26&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Base-10&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;td&gt;a&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;td&gt;z&lt;/td&gt;&lt;td&gt;26&lt;/td&gt;&lt;/tr&gt;
&lt;td&gt;aa&lt;/td&gt;&lt;td&gt;27&lt;/td&gt;&lt;/tr&gt;
&lt;td&gt;az&lt;/td&gt;&lt;td&gt;52&lt;/td&gt;&lt;/tr&gt;
&lt;td&gt;ba&lt;/td&gt;&lt;td&gt;53&lt;/td&gt;&lt;/tr&gt;
&lt;td&gt;zz&lt;/td&gt;&lt;td&gt;702&lt;/td&gt;&lt;/tr&gt;
&lt;td&gt;aaa&lt;/td&gt;&lt;td&gt;703&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;So, basically each position is a power of 26.  Pretty reasonable... I wanted a sub that I could easilly pass a string, and receive the base 10 equivalent.  The following is my sub and test program...&lt;/p&gt;
&lt;code&gt;
#!/usr/bin/perl

use warnings;
use strict;
use POSIX qw/ pow /;

sub convert_from_base26 {
   my ($total, $exp);
   my ($val) = @_;

   $total += (ord($_) - (ord('a') - 1)) * pow(26, $exp++)
      for split('', lc(reverse($val)));

   return $total;
}

print "Enter a value: ";
while (&lt;&gt;) {
   chomp;
   print convert_from_base26($_)."\n";
   print "Enter a value: ";
}
&lt;/code&gt;
&lt;p&gt;Although the code for the sub is relatively concise, it doesn't look like code I'd like to revisit (not to mention that I need to find a new sub name), as when I look at the code it's not "obvious" what is going on.  I think the term I heard at one point that I loved was: "it has too many metasyntatic variables."  So, monks... any ideas as to how I could write this to be a more elegant piece of code?&lt;/p&gt;
</field>
</data>
</node>
