G'day thanos1983,
Here's a recursive function that, I believe, achieves what you're after.
#!/usr/bin/env perl -l
use strict;
use warnings;
use constant BASE => ord('A') - 1;
use constant MAX => ord('Z') - BASE;
my @tests = (
0 .. 3, 24 .. 28,
26**2+24 .. 26**2+28,
26**3+26**2+24 .. 26**3+26**2+28,
);
print "IN: $_ OUT: ", translate($_) for @tests;
sub translate {
my ($in, $out) = @_;
$out ||= [];
return join '', @$out if $in == 0;
my $mod = $in % MAX || MAX;
unshift @$out, chr $mod + BASE;
my $new_in = ($in - $mod) / MAX;
translate($new_in, $out);
}
Output:
IN: 0 OUT:
IN: 1 OUT: A
IN: 2 OUT: B
IN: 3 OUT: C
IN: 24 OUT: X
IN: 25 OUT: Y
IN: 26 OUT: Z
IN: 27 OUT: AA
IN: 28 OUT: AB
IN: 700 OUT: ZX
IN: 701 OUT: ZY
IN: 702 OUT: ZZ
IN: 703 OUT: AAA
IN: 704 OUT: AAB
IN: 18276 OUT: ZZX
IN: 18277 OUT: ZZY
IN: 18278 OUT: ZZZ
IN: 18279 OUT: AAAA
IN: 18280 OUT: AAAB
"I am not posting the code here as it is irrelevant and will only consume space, ..."
A cutdown version might have provided some context.
Without that, I don't know exactly how you'd implement this solution.
You'll probably want some validation of the input.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.