I have 2 function in Lua, one to look if a key is present inside a provided table and if true then return the value, else return false. This lookup function is called by another function with a "priority queue". It is not a queue per definition but I don't have a better name for it.
function inTable(tbl, item)
for key, value in pairs(tbl) do
if key == item then
return value
end
end
return false
end
function ReturnString()
local valid = {}
valid[22] = "string1"
valid[13] = "string2"
valid[25] = "string3"
...
if inTable(valid, "22") then
return inTable(valid, "22")
elseif inTable(valid, "13") then
return inTable(valid, "13")
elseif inTable(valid, "25") then
return inTable(valid, "25")
else
print("nope not found")
end
end
Usage:
print(ReturnString())
valid is filled with a lot of different key-value pairs, unordered. My goal is to return the value based on a priority I determine using my nested if-elseif-else construct. If there is a key 22 then return string1, else check if there is a key 13 and so on. I know that there are more real implementation of a priority queue like this but I prefer something simple. What improvement can I do without implementing a complex priority queue?
"in your code. \$\endgroup\$