Skip to main content
ranjank0002
New Participant
Jan 4, 2024 11:05 AM UTC
Question

Need help on expression rule for array calculation

I am having an array like {22,10,12,11, 8, 17}. I need an expression which return the index where individual array value or sum of previous values is greater than or equal to 20.

For example: The output should be {1, 3, 5, 6}.

22 (22-20) => 2 (index = 1)

2 (remainder from previous value) + 10 + 12 (24-20) => 4 (index = 3)

4 (remainder from previous value) + 11 + 8 (23-20) => 3 (index = 5)

3 (remainder from previous value) + 17(20-20) => 0 (index = 6) 

As there is no while/do-while loops in Appian so it seems bit difficult to get the desired output with foreach loop.

5 replies

harshitb6843
Brainy
Jan 4, 2024 11:10 AM UTC

This is not straight-forward but doable. What are you trying to achieve? Maybe there is a better way to do that. 

ranjank0002
New Participant
Jan 4, 2024 11:22 AM UTC

Well, these values in array are number of rows / lines of a table or grid. So assuming 20 is the max size of rows a page can have, I need to add the page breaks if any of the grid is not having enough rows left of the page.

Inspiring
Jan 4, 2024 12:47 PM UTC

Not the most optimized solution, just gave it a try:

-Expression Rule Name: DA1_trialCommunity

-Rule Inputs: 

arr: List of Number(Integer)
prev: Number(Integer)
ans: List of Number(Integer)
index: Number(Integer)

if(
  a!isNullOrEmpty(ri!arr),
  ri!ans,
  a!localVariables(
    local!sum: ri!prev + ri!arr[1],
    local!rem: if(
      local!sum >= 20,
      local!sum-20,
      local!sum
    ),
    rule!DA1_trialCommunity(
      arr: ldrop(ri!arr,1),
      prev: local!rem,
      index: ri!index+1,
      ans: if(
        local!rem <> local!sum,
        append(ri!ans, ri!index),
        ri!ans
      )
    )
  )
)

You can further optimize it and also use reduce function.

Inspiring
Jan 4, 2024 12:33 PM UTC

You can try to use recursion to achieve this.