Skip to main content
Known Participant
May 20, 2021 1:54 PM UTC
Question

How to compare string elements

How to compare letters of a single word in Appian:

Suppose,

We have to compare letters of word "Apple". The output should be:

1. Number of unique characters in word. In case of "Apple", it should be 4

2. Could of similar characters. Ex: In case of "Apple", A=1,p=2,l=1,e=1

Please provide code for this.

    7 replies

    stefanhelzle0001
    Brainy
    May 20, 2021 2:05 PM UTC

    Is this some test? Similar question has been asked just recently. Please check the search function.

    ayushimAuthor
    Known Participant
    May 25, 2021 11:01 AM UTC

    Hi Stefan,

    It is not related to any test. I was learning about strings in Appian and this question strike in my mind.

    Thank you so much for your response. 

    davel001150
    Inspiring
    May 20, 2021 2:33 PM UTC

    First, my favorite method of breaking a text into an array of texts: char(code(local!text))

    Then, remove duplicate values with union, then grab the length().

    local!splitText: char(code(local!text)),

    length(union(local!splitText, local!splitText))

    To do the second one, you need to have (or make) a split alphabet.  Then a!forEach:

    a!forEach(

    items: local!splitAlphabet,

    expression: fv!item & wherecontains(fv!item, local!splitText)

    That's how I'd do it, or at least how I'd start.

    ayushimAuthor
    Known Participant
    May 25, 2021 11:03 AM UTC

    Thank you

    ranjiths0003
    Inspiring
    May 20, 2021 4:30 PM UTC

    Hi Ayushimitaal,

    Use Below Code

    a!localVariables(
      local!word: "apple",
      local!letters: apply(
        charat(local!word, _),
        enumerate(len(local!word)) + 1
      ),
      local!unique: union(local!letters, local!letters),
      a!flatten(
        a!forEach(
          local!unique,
          {
            fv!item & ": " & count(wherecontains(fv!item, local!letters))
          }
        )
      )
    )

    ayushimAuthor
    Known Participant
    May 25, 2021 11:03 AM UTC

    Thank you for your suggestion.