Looks like you are running afoul of the "special" argument handling that is done by the "print" function, when it tries to figure out whether its first arg is a file handle to print to, or simply the first value to be printed (to the current default output file handle) what ikegami said.

In any case, I think the better way to fix the original problem is like this:

sub v{ ord($_[0])-ord("A")+1 }
That is, your v() sub should return the correct index value for a given letter (between 1 and 26), so that it doesn't have to be fixed by the caller.

UPDATE: I wondered about your comment regarding "weight 1" vs. "weight 0"... are you sure about that? If we are talking about the column labels in a spreadsheet, "A" is col. 1, "Z" is 26, "AA" is 27, etc. That is, each letter "digit" has a "weight" of 1 (if I understand you terminology correctly).

In any case, an even better solution is to let the v() sub split the string and handle the power multiplication:

sub v { my (@letters) = split //, $_[0]; my $sum = 0; my $pwr = 1; while ( @letters ) { my $digit = ord( pop( @letters )) - ord("A") + 1; $sum += $digit * $pwr; $pwr *= 26; } return $sum; } print v("AB"),"\n";
prints "28", as it should, IMO.

In reply to Re: Converting "IV" from base 26 by graff
in thread Converting "IV" from base 26 by pc88mxer

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.