Skip to main content
Known Participant
Apr 29, 2021 12:06 PM UTC
Solved

Count the characters matching alphabets

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)

    csteward
    cstewardAnswer
    Brainy
    Apr 29, 2021 3:42 PM UTC

    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
            )
          )
        )
      )
    )

    7 replies

    csteward
    cstewardAnswer
    Brainy
    Apr 29, 2021 3:42 PM UTC

    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
            )
          )
        )
      )
    )

    Known Participant
    Apr 29, 2021 3:48 PM UTC

    Thank you. this works

    Known Participant
    Apr 29, 2021 4:00 PM UTC

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

    New Participant
    Oct 17, 2026 10:45 AM UTC

    a!localVariables(
      local!word: "caterpillar",
      local!split: a!foreach(
        items: enumerate(len(local!word)) + 1,
        expression: mid(local!word, fv!item, 1),
      ),
      local!uni: union(local!split,local!split),
      local!final: a!forEach(
        items:local!split,
            expression: 
            concat(fv!item & "" & count(wherecontains(fv!item,local!split))  ),
        
      ),
      joinarray(union(local!final,local!final),"")
      /*local!final*/
      
    )

    Inspiring
    Oct 19, 2026 3:11 PM UTC

    Your approach looks good. One small improvement would be to iterate over local!uni instead of local!split, since it already contains the distinct characters. This avoids processing duplicate characters and removes the need for the final union(). You can directly use joinarray() to return the result.

    a!localVariables(
    local!word: lower("caterpillar"),
    local!split: a!forEach(
    items: enumerate(len(local!word)) + 1,
    expression: mid(local!word, fv!item, 1)
    ),
    local!uni: union(local!split, local!split),
    joinarray(
    a!forEach(
    items: local!uni,
    expression: concat(
    fv!item,
    count(wherecontains(fv!item, local!split))
    )
    ),
    ""
    )
    )