Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: incrementing mixed letters and numbers

by shmem (Chancellor)
on Aug 22, 2017 at 08:36 UTC ( #1197790=note: print w/replies, xml ) Need Help??


in reply to incrementing mixed letters and numbers

As poj says. Heh... didn't know that there's a module for that. This is what I have in my toolbox

my %hash; my @chars = (0..9,'A'..'Z','a'..'z',map{chr$_}32..47,58..64,91..96 +); @hash{@chars} = 0..$#chars; sub encode_base { my ($base,$num) = @_; my ($rem,@ret); while ($num) { push @ret, $chars[($rem = $num % $base)]; $num -= $rem; $num /= $base; } return join '', reverse @ret; } sub decode_base { my ($base, $str) = @_; my $num; $num = $num * $base + $hash{$_} for $str =~ /./g; $num; }

which is basically the same, but lets you encode/decode with a base up to 91. So,

my $number = decode_base 36, 1009; say lc encode_base 36, $number++ for 0..36;

does what you want. Also, these subs can be used to convert from decimal to binary, octal, hex and reverse, using 2, 8 and 16 as base. But I'd use the perl builtins for that ;-)

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-12-11 03:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (286 votes). Check out past polls.

    Notices?