Hi,
I have a local variable with 2 items .
I need to add each element in the array with the corresponding index and display the total . Eg {1,2,3} +{1,2,3} = {2,4,6}
But in my case I can't apply the addition directly . It should be in a loop. I even tried using merge function but it is not working.. . How to achieve this. The below output is present inside a local variable (local!final).I need to add {0+2,36+82,2+8} and form a single array.
Discussion posts and replies are publicly visible
Did you try a!foreach()?
I am trying a!foreach but can't able to achieve the logic
Code?
This output is stored in a local variable local!final
I just want to continue from here. I have a local variable with 2 items and I need to add the elements inside the array . I want the output as 2,118,10
a!localVariables( local!values: a!forEach( items: enumerate(2), expression: if(fv!index = 1, {0,36,2}, {2,82,8}) ), a!forEach( items: enumerate(count(a!flatten(local!values[1]))), expression: a!localVariables( local!outerIndex: fv!index, sum( a!forEach( items: enumerate(count(local!values)), expression: local!values[fv!index][local!outerIndex] ) ) ) ) )
Hi Stefen,
The code is working for 2 elements. But in some scenarios I might have more than 2 elements based on length of array.
In the below scenario I have 4 elements in total, I have a local variable local!add containing all the elements.
I am trying the same code that you sent but the elements should not hardcoded.
local!length:length(local!add) local!ss:{local!add[1]}, local!ee:difference(local!add,local!ss), local!value:a!forEach( items: enumerate(local!length), expression: if(fv!index = 1, {local!ss}, {local!ee}) ), local!ff:a!forEach( items: enumerate(count(a!flatten(local!value[1]))), expression: a!localVariables( local!outerIndex: fv!index, sum( a!forEach( items: enumerate(count(local!value)), expression: local!value[fv!index][local!outerIndex] ) ) ) ),
As I understand it, there's no need to follow the definition of local!value. You can replace local!value with your own variable—the one on which you want to perform the calculation. You just need to copy this part accordingly. and it should work fine.a!forEach( items: enumerate(count(a!flatten(local!value[1]))), expression: a!localVariables( local!outerIndex: fv!index, sum( a!forEach( items: enumerate(count(local!value)), expression: local!value[fv!index][local!outerIndex] ) ) ) )
local!value
a!forEach( items: enumerate(count(a!flatten(local!value[1]))), expression: a!localVariables( local!outerIndex: fv!index, sum( a!forEach( items: enumerate(count(local!value)), expression: local!value[fv!index][local!outerIndex] ) ) ) )
I use this complicated way to generate the local only to keep that nested list structure. You already have the data in that format.