Update variable inside foreach loop

Hi Community

I have a case that I need to decrement counter in foreach loop

is it poosible ?

My code:

local!numbers: {2, 12, 14, 22, 32},

local!counter: 100,

local!loop: a!foreach(

     local!numbers,

     /* need here to decrement the counter */

)

  Discussion posts and replies are publicly visible

Parents Reply
  • +1
    Certified Lead Developer
    in reply to Ammar Alhayki

    OK. Check out this example.

    Each iteration subtracts the sum of the first few items in the list. enumerate() creates a list of number from 0 to n-1. Adding 1 turns it into 1 to n. index() then picks the values at these indexes.

    a!localVariables(
      local!numbers: {2, 12, 14, 22, 32},
      
      local!counter: 100,
      
      a!forEach(
        items: local!numbers,
        expression: local!counter - sum(index(local!numbers, (1 + enumerate(fv!index)), 0))
      )
    )

Children