Is there a function for recover the index(key) of a table, instead "loops" ?
5 replies, posted
Hi, Is there a function for recover the index(key) of a table, instead "loops" ?
Exemple :
[php]
Sett = {}
Sett.Tfunctions = {
first = function() print( "test" ) end,
second = function() print( "lol" ) end
}
[/php]
How to recover "first" and "second" index.
[tab]Thank you for your future answers.[/tab]
[lua]-- You can call those functions one of two ways:
-- 1. Use strings
Sett.Tfunctions["first"]()
Sett.Tfunctions["second"]()
-- 2. Use the name itself
Sett.Tfunctions.first()
Sett.Tfunctions.second()
[/lua]
You have to remember that in Lua tables, when you assign a value to a specific key that is not in [brackets], you can refer to it by name or by a string.
Ok, but I express myself badly, on loops :
[php]
for k,v in pairs(Sett.Tfunctions) do
print(k) //<- I want to recover 'k'
end
[/php]
But I don't want to use loops for my little script, it's for that I am looking for a simply function, if it exsits.
If the table's dynamic, though?
[QUOTE=batman06;22493778]Ok, but I express myself badly, on loops :
[php]
for k,v in pairs(Sett.Tfunctions) do
print(k) //<- I want to recover 'k'
end
[/php]
But I don't want to use loops for my little script, it's for that I am looking for a simply function, if it exsits.[/QUOTE]
Unfortunately a loop is the only way to do what I think you want to do.
Sorry, you need to Log In to post a reply to this thread.