I have a table, mytbl.
This table contains other tables that contain strings, so something like this: [CODE]local mytbl = {}
mytbl.one = {}
mytbl.two = {}
mytbl.one["FirstKey"] = "FirstValue"
mytbl.one["SecondKey"] = "SecondValue"
mytbl.one["ThirdKey"] = "ThirdValue"
mytbl.two["Key1"] = "Value1"
mytbl.two["Key2"] = "Value2"
mytbl.two["Key3"] = "Value3"[/CODE]
There are more items per table and more tables, no key or value is ever repeated.
I am wondering what is the most efficient way to check for if a table contains a [B][I]key[/I][/B] (Doesn't matter which table)
I already know I can do something like this: [CODE]for _, tbl in pairs( mytbl ) do
for k, v in pairs( table.GetKeys( tbl ) do
if v == ThingToCheck then
--Stuff
end
end
end[/CODE]
But is there a better way?
[QUOTE=JasonMan34;49732463]I have a table, mytbl.
This table contains other tables that contain strings, so something like this: [CODE]local mytbl = {}
mytbl.one = {}
mytbl.two = {}
mytbl.one["FirstKey"] = "FirstValue"
mytbl.one["SecondKey"] = "SecondValue"
mytbl.one["ThirdKey"] = "ThirdValue"
mytbl.two["Key1"] = "Value1"
mytbl.two["Key2"] = "Value2"
mytbl.two["Key3"] = "Value3"[/CODE]
There are more items per table and more tables, no key or value is ever repeated.
I am wondering what is the most efficient way to check for if a table contains a [B][I]key[/I][/B] (Doesn't matter which table)
I already know I can do something like this: [CODE]for _, tbl in pairs( mytbl ) do
for k, v in pairs( table.GetKeys( tbl ) do
if v == ThingToCheck then
--Stuff
end
end
end[/CODE]
But is there a better way?[/QUOTE]
[CODE]if mytbl["key"] != nil then[/CODE]
[QUOTE=tzahush;49732540][CODE]if mytbl["key"] != nil then[/CODE][/QUOTE]
The keys for mytbl are not the keys I want to check.
The keys for mybl are one/two/three etc.
[QUOTE=JasonMan34;49732599]The keys for mytbl are not the keys I want to check.
The keys for mybl are one/two/three etc.[/QUOTE]
so you want to see if lets say, mytbl.one has a specific key? then do
[code]if mytbl.one["key"] "Your check here, like != or ==" blah then[/code]
[QUOTE=whitestar;49732642]so you want to see if lets say, mytbl.one has a specific key? then do
[code]if mytbl.one["key"] "Your check here, like != or ==" blah then[/code][/QUOTE]
I want to check if ANY sub-table has a key. Not just mytbl.one
[QUOTE=JasonMan34;49732684]I want to check if ANY sub-table has a key. Not just mytbl.one[/QUOTE]
I can only think of a opposite mapping such as
mytbl.one["Key"] = "Hello World"
mytbl.Keys["Key"] = mytbl.one
So if you need a table for a specific key you can use local tab = mytbl.Keys["Key"] and get the table
"one"
Thats the fastest way I can think about, eventually you have to reconsider your data structure.
I have always wondered whether opposite mapping is better than more functions, almost like more memory vs more stress (but they're both minimal). Regardless, the other way than opposite mapping you could do it would be to make a function.
[lua]function FindKeyInTableTree(key)
for k,v in pairs(mytbl) do
for k2,v2 in pairs(v) do
if k2 == key then return true end
end
end
return false
end[/lua]
I ended up doing something else entirely, fits my needs better anyways.
Thanks for the help
Sorry, you need to Log In to post a reply to this thread.