Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Split int into segments of max int 26

by kcott (Chancellor)
on Sep 13, 2017 at 04:43 UTC ( #1199254=note: print w/replies, xml ) Need Help??


in reply to Split int into segments of max int 26

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.

— Ken

Replies are listed 'Best First'.
Re^2: Split int into segments of max int 26
by thanos1983 (Priest) on Sep 13, 2017 at 07:54 UTC

    Hello kcott,

    This is exactly what I was trying to accomplish yesterday but for some reason I was so tired of thinking even.

    Thanks for sharing, great input.

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1199254]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-10-20 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (260 votes). Check out past polls.

    Notices?