The basic base-26 conversion doesn't work right immediately (you get ...Y,AZ,AA,AB...), but a well placed decrement fixes it
sub ConvertToAlpha {
my $self = shift;
my $inNumber = shift;
my @output = ();
while ($inNumber > 0) {
unshift(@output, ($inNumber % 26));
my $shouldDecrement = ($inNumber %26 == 0);
use integer;
$inNumber = $inNumber / 26;
if ($shouldDecrement) {
$inNumber--;
}
}
# @output-1 since we won't ever need to change the last digit
my %toAlpha = ( 1=>'A', 2=>'B', 3=>'C',
4=>'D', 5=>'E', 6=>'F',
7=>'G', 8=>'H', 9=>'I',
10=>'J', 11=>'K', 12=>'L',
13=>'M', 14=>'N', 15=>'O',
16=>'P', 17=>'Q', 18=>'R',
19=>'S', 20=>'T', 21=>'U',
22=>'V', 23=>'W', 24=>'X',
25=>'Y', 0=>'Z');
foreach (@output) {
$_=$toAlpha{$_};
}
join("",@output);
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.