Skip to main content
Known Participant
Feb 15, 2024 5:44 AM UTC
Question

Getting Indices from an Array of Number

Hi,

We have an expression rule (see below image). We want to get the indices of numbers from the array (local!numberList) that would not make the local!initialValue negative when added together.

 

The result we are expecting from the sample values in the image is: {1, 2, 3, 5}

If we have local!initialValue = 100 & local!numberlist = {5, 100, 15, 100, 25}, the result should be: {1, 3, 5}

We're having a hard time thinking of the way we can do this. Would gladly appreciate your help.

    10 replies

    boddupallib637791
    Known Participant
    Feb 15, 2024 5:50 AM UTC

    Can you ellobrate what does added together means?

    Known Participant
    Feb 15, 2024 5:55 AM UTC

    Let's say for iteration 3, we are indexing item 1, 2, and 3 from the local!numberList, which value is {5, 10, 15, 100, 25}, so the value that the sum() function would return in this iteration is 30. 100 - 30 = 70, so in this case the index should be returned.  

    stefanhelzle0001
    Brainy
    Feb 15, 2024 8:43 AM UTC

    I think this calls for a recursive approach. I wrote a blog post about implementing algorithms in Appian.

    https://appian.rocks/2022/08/29/complex-algorithms-in-appian/

    Known Participant
    Feb 15, 2024 11:02 AM UTC

    Thank you for sharing this.

    Participating Frequently
    Feb 15, 2024 8:57 AM UTC

    Code

    a!localVariables(
      local!a:100,
      local!array:{5,100,15,100,25},
      reject(
        fn!isnull,
        a!forEach(
        items: local!array,
        expression: a!localVariables(
          local!Boolean:if(
            fv!isFirst,
            {fv!item<100},
            a!forEach(
              items: enumerate(fv!index-1)+1,
              expression: local!a-sum(index(local!array,enumerate(fv!item)+1,0))>0
            )
          ),
          local!currentItem:fv!item,
          local!BooleanSum:if(
            fv!isFirst,
            0,
            sum(a!forEach(
            items: local!Boolean,
            expression: if(
              fv!item,
              index(local!array,fv!index,{}),
              0
            )
          ))
          ),
          if(
            local!a-sum(local!BooleanSum,local!currentItem)>0,
            fv!index,
            null()
          )
        )
      )
      )
    )

    result

    Known Participant
    Feb 15, 2024 9:54 AM UTC

    Woah, I didn't know this. Thank you so much!