in reply to Re: Increment a mixed alphanumeric (Updated)
in thread Increment a mixed alphanumeric

Right, here is a regexp for valid formats:
/^[A-Z]+[0-9]*[A-Z]*/;
N1234Z becomes N12350
N12359 becomes N1235A
N9999Z becomes NA0000

Ordering of the addition is less important than testing all possible variations. Technically NA0000 is invalid since it has a number, but the number part equals zero. I can handle that logic outside of this code construct.

Replies are listed 'Best First'.
Re^3: Increment a mixed alphanumeric (Updated)
by BrowserUk (Patriarch) on Oct 27, 2015 at 15:38 UTC

    By extension:

    #! perl -slw use strict; =comment N1234Z becomes N12350 N12359 becomes N1235A N9999Z becomes NA0000 =cut my $id = "F00123Z"; for ( 1 .. 80 ) { print $id; last if substr( $id, -5 ) eq '9999Z'; if( substr( $id, -1 ) eq 'Z' ) { substr( $id, -1 ) = '0'; substr( $id, 1, -1 )++; } elsif( substr( $id, -1 ) eq '9' ) { substr( $id, -1 ) = 'A'; } else { substr( $id, -1 ) ++; } }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.