For a particular task I am trying to accomplish, I require the ability to quickly check for the existence of a variable using table[variable_name] for quick confirmation that it exists. I use this pretty frequently. Recently, however, I've learned that if I kept tabs on the order of this table, I would be able to accomplish another task. The thing is, the keys can be deleted after they have existed for a set amount of time, or if I clear them before then (I use timers for this).
The problem this creates is, if I delete some keys out of order, to delete them in the corresponding numerically keyed table, I would need to loop through it, compare the names, and table.remove.
I just want to reduce the performance cost, since I do this fairly frequently.
TO illustrate in code what I mean:
I would create a key and do:
but when I'm reading to clear it, I have to do:
Is there any way I can try to maintain a semblance of order and easily add and remove entries without having to resort to looping through the ordered table and picking off entries? I admit I know little about lua and performance, but I imagine looping through like this as often as I may need to call this will get a little costly.
The problem this creates is, if I delete some keys out of order, to delete them in the corresponding numerically keyed table, I would need to loop through it, compare the names, and table.remove.
I just want to reduce the performance cost, since I do this fairly frequently.
TO illustrate in code what I mean:
I would create a key and do:
table["dog"] = true
table.insert(reference_table, "dog")
but when I'm reading to clear it, I have to do:
table["dog"] = nil
for n,i in ipairs(reference_table) do
if i == "dog" then
table.remove(reference_table, n)
end
end
Is there any way I can try to maintain a semblance of order and easily add and remove entries without having to resort to looping through the ordered table and picking off entries? I admit I know little about lua and performance, but I imagine looping through like this as often as I may need to call this will get a little costly.