Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Greetings!

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!" :)

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...

Base-26Base-10
a1
z26
aa27
az52
ba53
zz702
aaa703

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...

#!/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 (<>) { chomp; print convert_from_base26($_)."\n"; print "Enter a value: "; }

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?


In reply to Alpha base-26 to base-10... by eduardo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2015-07-07 06:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (87 votes), past polls