How to Auto Increment Alpha Characters in an expression rule to create sequential ID's

Hello,

I'm looking for a way to automatically increment a set of 4 alpha characters given a rule input of an existing set of 4 characters.

 

For example if the rule is passed AAAA it returns AAAB, if the rule is passed AABA it returns AABB, if the rule is passed ABBZ it returns ABCA, etc.  And finally if the rule is passed ZZZZ it returns AAAA.  Any help on this would be greatly appreciated. Thank you!

 

I've been thinking maybe a constant with an array of the alphabet, an apply that sets a z flag to false at the beginning, checks position 4 for what letter it is using a wherecontains() and increment it by one, if Z set it to A and set z flag to true, then move on to position 3 ONLY when Z flag was true, run through same loop until z flag is false at the check. using choose() to tell it what letter to set to next.

  Discussion posts and replies are publicly visible

Parents
  • This allows you to make this an extremely dynamic rule - by using reduce()

    load(
    reduce(
    rule!jcj_testingAlphaIncrementer(

    newId:_,
    position:_,
    allowedLetters: ri!allowedLetters,
    lastIdAlpha:ri!lastIdAlpha
    ),
    {},
    reverse(enumerate(len(ri!lastIdAlpha))+1)
    )
    )

     

    with(
    local!letter: index(ri!lastIdAlpha, ri!position, null),
    local!index: wherecontains(local!letter, ri!allowedLetters),
    local!nextLetter:
    if(
    or(
    rule!APN_isBlank(ri!lastIdAlpha),
    local!index = count(ri!allowedLetters)
    ),
    index(ri!allowedLetters, 1, null),
    index(ri!allowedLetters, local!index+1, null)
    ),
    local!indexLowerLetter:
    index(ri!allowedLetters,
    wherecontains(index(ri!newId, ri!position+1, null), ri!allowedLetters),
    null
    ),

    if(ri!position = count(ri!allowedLetters),

    replace(ri!lastIdAlpha, ri!position, 1, local!nextLetter),

    if(
    local!indexLowerLetter = "A",
    replace(ri!newId, ri!position, 1, local!nextLetter),
    ri!newId
    )
    )
    )

Reply
  • This allows you to make this an extremely dynamic rule - by using reduce()

    load(
    reduce(
    rule!jcj_testingAlphaIncrementer(

    newId:_,
    position:_,
    allowedLetters: ri!allowedLetters,
    lastIdAlpha:ri!lastIdAlpha
    ),
    {},
    reverse(enumerate(len(ri!lastIdAlpha))+1)
    )
    )

     

    with(
    local!letter: index(ri!lastIdAlpha, ri!position, null),
    local!index: wherecontains(local!letter, ri!allowedLetters),
    local!nextLetter:
    if(
    or(
    rule!APN_isBlank(ri!lastIdAlpha),
    local!index = count(ri!allowedLetters)
    ),
    index(ri!allowedLetters, 1, null),
    index(ri!allowedLetters, local!index+1, null)
    ),
    local!indexLowerLetter:
    index(ri!allowedLetters,
    wherecontains(index(ri!newId, ri!position+1, null), ri!allowedLetters),
    null
    ),

    if(ri!position = count(ri!allowedLetters),

    replace(ri!lastIdAlpha, ri!position, 1, local!nextLetter),

    if(
    local!indexLowerLetter = "A",
    replace(ri!newId, ri!position, 1, local!nextLetter),
    ri!newId
    )
    )
    )

Children
  • Thanks to immense help from Jackie, here's the set of rules that seem to work for this as best as I've tested so far. (In case anyone else ever needs it)

    ri!allowedLetters is a passed in constant with a text array of letters allowed to be used for incrementation
    ri!inputString is the original alpha string passed in

     

    load(
      
      local!newString: null,
      
      local!arrayWithString:
        rule!GBL_incrementAlphaString(
          allowedLetters: ri!allowedLetters,
          inputString: ri!inputString,
          newString: local!newString
        ),
        
      local!updatedString:
        index(
          local!arrayWithString,
          count(local!arrayWithString), null
        ),
        
      local!updatedString
    )

     

    GBL_incrementAlphaString

      reduce(
        rule!GBL_alphaStringIncrementer(
          newString:_,
          position:_,
          allowedLetters: ri!allowedLetters,
          inputString: ri!inputString
        ),
        {},
        reverse(enumerate(len(ri!inputString))+1)
      )
    

     

    GBL_alphaStringIncrementer

    with(
      local!letter: index(ri!inputString, ri!position, null),
      local!index: wherecontains(local!letter, ri!allowedLetters),
      local!nextLetter: 
        if(
          or(
            rule!APN_isBlank(ri!inputString),
            local!index = count(ri!allowedLetters)
          ),
          index(ri!allowedLetters, 1, null),
          index(ri!allowedLetters, local!index+1, null)
        ),
    
      local!indexLowerLetter: 
        index(ri!allowedLetters,
          wherecontains(index(ri!newString, ri!position+1, null), ri!allowedLetters),
          null 
        ),
        
      local!lastPositionChangedFromZ: 
        toboolean(index(ri!newString, count(ri!newString)-1, null)),
      {
        local!index = count(ri!allowedLetters),
        
        if(
          ri!position = len(ri!inputString),
        
          replace(ri!inputString, ri!position, 1, local!nextLetter),
          
          
          if(
            local!lastPositionChangedFromZ,
            replace(index(ri!newString, count(ri!newString), null), ri!position, 1, local!nextLetter),
            ri!newString
          )
        )
      }
    )