Piorun Posted February 9, 2012 Share Posted February 9, 2012 Hi. I have table table = { [1] = {"something", 30}, [2] = {"something", 50} } and i want to make something to add numeric values like table[1][2] + table[2][2] + ... + table[n][2] = x but using lua functions. How to do? Sorry for my bad eng Link to comment
drk Posted February 9, 2012 Share Posted February 9, 2012 table[1] ... table[2] ... For table[1][2] + table[2][2] + ... + table[n][2] = x You can't add tables with tables lol Learn: http://lua-users.org/wiki/TablesTutorial Link to comment
50p Posted February 9, 2012 Share Posted February 9, 2012 Hi. I have table table = { [1] = {"something", 30}, [2] = {"something", 50} } and i want to make something to add numeric values like table[1][2] + table[2][2] + ... + table[n][2] = x but using lua functions. How to do? Sorry for my bad eng There is no function to do that, unless you write it. function mySecretFunctionToAddNumericValuesFrom2DTable( tab ) local sum = 0; for k, subTab in pairs( tab ) do sum = sum + subTab[ 2 ]; -- subTab is the same as: tab[ k ] which is the same as: tab[ 1 ], tab[ 2 ], tab[ 3 ], etc. end return sum; end -- Then just call the function with 2D table: local sum = mySecretFunctionToAddNumericValuesFrom2DTable( myTable ); Draken, if you look closely, he's adding numbers that are held inside tables. Link to comment
Piorun Posted February 10, 2012 Author Share Posted February 10, 2012 Thanks .. i dont think so it's too easy . It help me a lot. Thanks once again. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now