• Accessing Table Indexes?
    6 replies, posted
I was wondering, how do I access the indexes of things in a table, like for accessing a table is there a way to get a string of the index for a specified variable?
They aren't documented yet, but table.KeyFromValue(Table, Value) and table.KeysFromValue(Table, Value). They latter returns a table of keys, the former, a single key.
Wow, didn't know about those. Thanks.
[QUOTE=PortalGod;28644978]They aren't documented yet, but table.KeyFromValue(Table, Value) and table.KeysFromValue(Table, Value). They latter returns a table of keys, the former, a single key.[/QUOTE] Why wouldn't it just be one function that always returns a table? Seems more logical that way.
Or you could loop through the table and check values, which is how I assume that function works.
[QUOTE=Jo The Shmo;28645666]Why wouldn't it just be one function that always returns a table? Seems more logical that way.[/QUOTE] Because if your just looking for one it's more efficient to stop there instead of continuing searching. [QUOTE=TheNerdPest14;28644836]I was wondering, how do I access the indexes of things in a table, like for accessing a table is there a way to get a string of the index for a specified variable?[/QUOTE] Rather than thinking you need a function to do this every time. It would be more useful to understand how to get it yourself. [LUA] for key, value in pairs(Table)do //Key, is what index it is looping through. //Value, is the value that is stored in that index. if(value==specificvariable)then //This is the key index we are looking for. end end [/LUA]
Just so everyone knows, source of the functions: [lua] function table.KeyFromValue( tbl, val ) for key, value in pairs( tbl ) do if ( value == val ) then return key end end end function table.KeysFromValue( tbl, val ) local res = {} for key, value in pairs( tbl ) do if ( value == val ) then table.insert( res, key ) end end return res end[/lua]
Sorry, you need to Log In to post a reply to this thread.