gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
-- Initial table local table = { [1] = {['item1'] = 1, ['item2'] = 2, ['item3'] = 3,…
- User
-- Initial table local table = { [1] = {['item1'] = 1, ['item2'] = 2, ['item3'] = 3, ['item4'] = 4}, [2] = {['item1'] = 1, ['item2'] = 2, ['item3'] = 3, ['item4'] = 4} }
Lua how can I get the sum of all the values in the sub table of [1]
- Assistant
You can use a loop to iterate through the subtable and sum the values:
lualocal sum = 0 for k, v in pairs(table[1]) do sum = sum + v end print(sum) -- Output: 10