Skip to content

Instantly share code, notes, and snippets.

@josevh
Created May 9, 2018 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josevh/6e3d60ae8edd61de78dcc32e1c2fa00e to your computer and use it in GitHub Desktop.
Save josevh/6e3d60ae8edd61de78dcc32e1c2fa00e to your computer and use it in GitHub Desktop.
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