Created
May 9, 2018 16:47
-
-
Save josevh/6e3d60ae8edd61de78dcc32e1c2fa00e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub alphanumeric_increment { | |
my $string = "" . shift; | |
my $position = @_ ? shift : -1; | |
if ($position == -1) { | |
$position = (length $string) - 1; | |
} | |
my $increment_str = substr $string, $position, 1; | |
if ($increment_str eq '9') { | |
substr $string, $position, 1, 'a'; | |
} elsif ($increment_str eq 'z') { | |
substr $string, $position, 1, 'A'; | |
} elsif ($increment_str eq 'Z') { | |
if ($position == 0) { | |
substr $string, $position, 1, '0'; | |
$string .= '0'; | |
} else { | |
my $inc_position = $position - 1; | |
$string = alphanumeric_increment($string, $inc_position); | |
substr $string, $position, 1, '0'; | |
} | |
} else { | |
$increment_str++; | |
substr $string, $position, 1, "".$increment_str; | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment