lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hi all.

I have a little doubt. I have a lua table with some values and I neet the sum of them.

Here's is the code snippet:
c = 0
function sum(v)
c = c+v

values ={1,2,3,4,5,6,7,8}

result = 0
for _,value in values do
result = result + value


The code between result = 0 and print(result) works, but i don't think that this is the 'lua way' of doing the things. Except by the lua way of make the for loop, this could be C, PHP or _javascript_.

The function sum is my attempt to make it more 'lua'. It works, but all the values are printed and I have the c variable outside of the scope of the function (global). As far I know, I can do it with a closure no ?

I just wanna make my lua code more lua :)

Thanks on advance.

- Walter