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 5 years ago

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

Weird Example:

local t = {1, 4, 6, 9}

print(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 5 years ago
Edited 5 years ago
local t = {1,4,6,9} -- table

local total = 0 -- variable for the total

for i,v in pairs(tab) do -- iterate through the table and add on to the total
    total = total + v
end

print(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