in reply to Re: Algorithm for "Incrementing" strings
in thread Algorithm for "Incrementing" strings

Oops, typo

$x =~ s/([^$last]?)($last*)$/ $next{$1} . $first x length $2 /e;

Replies are listed 'Best First'.
Re^3: Algorithm for "Incrementing" strings (single regex)
by tye (Sage) on Feb 15, 2015 at 03:51 UTC

    The general "increment a string" algorithm popped into my head much later so I checked back and was happy to see that it had already been posted. Here is my formulation of it that requires no set-up (easy for a human to compose when told what "digits" to use but not appropriate to use if the digits are not pre-set):

    s{([^Z])?(Z*)$}{ local $_ = ( $1 // 'Z' ) . $2; tr/BCDFGHJ-NP-TV-Z/CDFGHJ-NP-TV-ZB/; $_ }e;

    Decrement:

    s{(([^B])|B)(B*)$}{ local $_ = ( $2 // '' ) . $3; tr/CDFGHJ-NP-TV-ZB/BCDFGHJ-NP-TV-Z/; $_ }e;

    - tye