Skip to main content
April 29, 2021
Solved

Count the characters matching alphabets

  • April 29, 2021
  • 5 replies
  • 0 views

Hello,

how to build an expression rule  which has 2 inputs - 1 is string, 2nd is alphabet. print the count of characters matching alphabet with the string

(Umbrella to expected Result -u1m1b1r1e1l2a1)

    Best answer by csteward

    Try this expression, it should work for you assuming you want to only return the first instance (with count) of each character.  E.g., "Caterpillar" = "c1a2t1e1r2p1i1l2".

    a!localVariables(
      local!alphabet: "abcdefghijklmnopqrstuvwxyz",
      local!input: "Umbrella",
      
      local!inputArray: a!forEach(
        items: 1+enumerate(len(local!input)),
        expression: lower(charat(local!input,fv!index))
      ),
      local!chars: union(local!inputArray,local!inputArray),
      
      joinarray(
        reject(
          fn!isnull,
          a!forEach(
            items: local!chars,
            expression: if(
              find(fv!item,local!alphabet,1)>0,
              concat(
                fv!item,
                count(wherecontains(fv!item,local!inputArray))
              ),
              null
            )
          )
        )
      )
    )

    5 replies

    csteward
    cstewardAnswer
    April 29, 2021

    Try this expression, it should work for you assuming you want to only return the first instance (with count) of each character.  E.g., "Caterpillar" = "c1a2t1e1r2p1i1l2".

    a!localVariables(
      local!alphabet: "abcdefghijklmnopqrstuvwxyz",
      local!input: "Umbrella",
      
      local!inputArray: a!forEach(
        items: 1+enumerate(len(local!input)),
        expression: lower(charat(local!input,fv!index))
      ),
      local!chars: union(local!inputArray,local!inputArray),
      
      joinarray(
        reject(
          fn!isnull,
          a!forEach(
            items: local!chars,
            expression: if(
              find(fv!item,local!alphabet,1)>0,
              concat(
                fv!item,
                count(wherecontains(fv!item,local!inputArray))
              ),
              null
            )
          )
        )
      )
    )

    April 29, 2021

    Thank you. this works

    April 29, 2021

    Chris, Can you please give the high level of the  code design which you have given. So that it can be clear.