Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you find the sum of all numbers in a table?

Asked by 6 years ago

How do you find add the numbers together that are in one table?

Weird Example:

1local t = {1, 4, 6, 9}
2 
3print(t[i] + t[i])

The answer to this would be "20".

I have no clue on how to do that. Does anyone know how to add up all the numbers in a single table or arrangement?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
1local t = {1,4,6,9} -- table
2 
3local total = 0 -- variable for the total
4 
5for i,v in pairs(tab) do -- iterate through the table and add on to the total
6    total = total + v
7end
8 
9print(total)

I don't know if there is any other way to do this, as I am not very good with tables, but this works.

Ad

Answer this question