 # Sum n Arrays (or Lists) in Coda

I need help to sum arrays (or Lists).

For example, I have these arrays: I make this formula to sum up to 5 arrays, but every time I get a bigger list of arrays I need to change the formula: I tried to use 2 FormulaMaps, but it always get the wrong CurrentValue, I just can reach the nearst CurrentValue.

Anyone can help, or it’s a Coda limitation?

@Welley_Rezende

Nesting `CurrentValues` has some limitations that are being addressed by the team. There are some mega threads about it in the forum, if you want to learn more.

In the screenshot you present here, it’s unclear to me what exactly is your desired output.

That said, see if the formula in the blue column moves you forward in some way.

@Ander I think the desired output is to sum all first elements of each array, all second elements, all third etc.

@Welley_Rezende I am pretty much sure that this is a data design problem, and the solution would be to organize the data differently, ideally in a way that each of the arrays’ elements was a value on a separate row: Or at least imported in an already transposed way, i.e. `[1,0,1,2][0,0,10,-5][5,-5,5,10]`, so that it’s easier to `.FormulaMap(CurrentValue.Sum())`

However, let’s say for some reason this is the only way you can have it. If the number of elements in each sub-array is the same, here’s what I’d do:

1. Flatten the array with `ListCombine()`. E.g. in your case this will result in a list with 12 elements.
2. Get the number of elements in each of the arrays (in your case 3)
3. Set up the outer `Sequence(1, Elements count)` so that you have 1, 2, 3.
4. Set up the inner `Sequence(CurrentValue, Total count, Elements count)` so that you have a sequence starting from 1 or 2 or 3, running up to 12, but with an increment of 3 and not 1. This will give you numbers: [1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12].
5. Within the inner sequence’s formula map, get the `Nth(CurrentValue)` element of the flattened list. This will result in a “transposed” array. You can `Sum()` right away.

Easier shown than explained:

TL;DR: the inner loop will give you numbers with the step of 3, with starting number shifted by 1 each time. These are the indices that you then can use in Nth.

For the record, if you remove `.Sum()`, that’s how you transpose that 2-dimensional array:

2 Likes

Thanks again @Paul_Danyliuk as always giving the opportunity to learn. 2 Likes

Thanks @Paul_Danyliuk! And Thanks @Ander too!

You’re right @Paul_Danyliuk, my desired output was to sum all first elements of each array, all second elements, all third etc. (like in the SumProduct formula). And well, actually, there’s a reason to this be the only way I can have this data.

Thanks again to help me, even if it looked like a data design problem. Besides that, I was able to learn how to transpose. 2 Likes