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()?
a!localVariables( local!first: { 1, 2, 3 }, local!sen: { 4, 5, 6 }, local!output: a!forEach(items: local!first, expression: fv!item) + a!forEach(items: local!sen, expression: fv!item), local!output)
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
Hii srinivaasant574373 ,
Here is the solution which can cover you use case,
a!localVariables( local!a: { 0, 36, 2 }, local!b: { 2, 82, 2 }, local!arrayWithTheLessSize: if( length(local!a) <= length(local!b), local!a, local!b ), a!forEach( items: local!arrayWithTheLessSize, expression: fv!item + index(local!b, fv!index, null) ) )
Thanks
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] ) ) ) ) )
In my usecase I have both the arrays in a single local variable (local!final)
Like this
I need to perform the addition by iterating this local variable.
You can try something like this! In your case the output is in local!a in the sample code below. you can ignore the other two variables local!x and local!y. Line 5 to 7 has actual solution.
a!localVariables( local!x: { 0, 36, 2 }, local!y: { 2, 82, 8 }, local!a: a!foreach({ 1, 2 }, if(fv!item = 1, local!x, local!y)), a!foreach( local!a[1], sum(fv!item, local!a[2][fv!index]) ) )
Hi You can index each items in a local variable and perform addition in a!foreach. For example if you have two items you can follow this code->
a!localVariables( /*ignore below code (replicating ur array)*/ local!a: { 0, 2, }, local!b: { 36, 82 }, local!c: { 2, 8 }, local!arr: merge(local!a, local!b, local!c), /*ignore above*/ /*code for getting value in seprate varables*/ local!arr1: index(local!arr, 1, ""), local!arr2: index(local!arr, 2, ""), /*addition*/ local!arrayWithTheLessSize: if( length(local!arr1) <= length(local!arr2), local!arr1, local!arr2 ), local!arrayWithTheMoreSize: if( length(local!arr1) > length(local!arr2), local!arr1, local!arr2 ), a!forEach( items: local!arrayWithTheMoreSize, expression: fv!item + index( local!arrayWithTheLessSize, fv!index, 0 ) ) )