miketosh has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to increment a string matching /[A-Z][A-Z0-9]+/ but cannot use the ++ operator, since the last character could be A-Z. My needs are to make sure I haven't seen this string before, and keep the first character the same. Is there a way to convert to a number, increment, and convert back to a string?
Here is what I have so far, that I know doesn't work:
+101 $used{"FOO123Z"} = 1; ..snip.. +323 my $new="FOO123Z"; +324 my $len = length($new); +325 while ( defined $used{$new} && $len > 0 ){ ## Keep the +first character the same +326 foreach my $c (("A" .. "Z"), 0..9){ +327 $new =~ s/(.{$len}).(.*)/${1}${c}${2}/ if defined $u +sed{$new}; +328 } +329 $len-- if defined $used{$new} ; +330 } +331 $used{$new} = 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Increment a mixed alphanumeric (Updated)
by BrowserUk (Patriarch) on Oct 27, 2015 at 14:18 UTC | |
by miketosh (Acolyte) on Oct 27, 2015 at 14:56 UTC | |
by BrowserUk (Patriarch) on Oct 27, 2015 at 15:38 UTC |