[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: just a little question about the lua way :)
- From: Walter Cruz <walter.php@...>
- Date: Wed, 14 Dec 2005 11:28:25 -0300
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
print(c)
end
values ={1,2,3,4,5,6,7,8}
result = 0
for _,value in values do
result = result + value
end
print(result)
table.foreach(values,sum)
_________
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.
[]'s
- Walter