Hello,

I have integer numbers represented in base 36 (each digit may have one of 36
values: ('0'..'9','A'..'Z')).
For example: '5BFHK'.
I need to increment the number by 1 or by some step (for example, by 25).
Here is my first take on incrementing by 1:

# -----------------------------
# incrementing value in base 36
# -----------------------------

my %next = (); 
@next{('0'..'9','A'..'Z')} = ('1'..'9','A'..'Z','0');

sub advance {
  my $num = shift;
  my @ar = split//,$num;
  $ar[-1] = $next{$ar[-1]};
  if($ar[-1] eq '0'){ 
    my $flag=1;
    for my $n (2..@ar) {
      $ar[-$n] = $next{$ar[-$n]};
      if($ar[-$n] ne '0') {$flag=0;last;}
    }
    unshift(@ar,'1') if($flag);
  }
  return join "", @ar;
}

for (qw(5BFHK 111Z ZZZZ)) { print "\n$_\n",advance($_),"\n"; }

__END__

outputs:

5BFHK
5BFHL

111Z
1120

ZZZZ
10000


Any advice?

Warmest Regards, 
Lev Selector, NY, (212) 902-3040

Reply via email to