Easiest way to go from what you have, would be to add a for loop to cause
you sub routine to loop the correct number of times.

#!c:/Perl/bin/perl
# -----------------------------
# incrementing value in base 36
# -----------------------------

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

print "Increment by what amount? \n";
chomp ($inc = <STDIN>);         # <<<<< get inputted number.

sub advance {
  my $num = shift;
  my @ar = split//,$num;
  for (my $x=0;$x<$inc;$x++) {  # loop thru the number of time you inputted.
    $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"; }


 -----Original Message-----
From:   Selector, Lev Y [mailto:[EMAIL PROTECTED]]
Sent:   Friday, October 11, 2002 3:57 PM
To:     [EMAIL PROTECTED]
Subject:        incrementing values in base 36

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