How do you find the sum of all the numbers in an array in Livecode?

450 views Asked by At

I'm having a problem finding the sum of all of the integers in an array in Livecode

2

There are 2 answers

0
dunbarx On BEST ANSWER

The "sum" command is what you need. Check out the dictionary; arrays are supported, though the function is generally used with a comma delimited list.

You can also extract the keys of the array, if they are purely numeric, and apply the function. Otherwise, you may have to extract the nested array of elements of interest into the clear with the "combine" command, and sum that.

Craig Newman

0
Devin On

This should work:

put 13 into tArray[1]
put 4 into tArray[2]
put -9 into tArray[3]
put 21 into tArray[4]
put sum(tArray) into tSumVar

Or alternatively:

put the keys of tArray into tKeyList
replace return with comma in tKeyList
put sum(tKeyList) int tSumVar