Jump to content

About tables


Piorun

Recommended Posts

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 :D

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...