5
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 8, 2015 at 13:54
\$\endgroup\$
1
  • 3
    \$\begingroup\$ There is a missing " in your code. \$\endgroup\$ Commented May 8, 2015 at 14:40

1 Answer 1

3
\$\begingroup\$

welcome to Code Review.

I don't understand the purpose of your inTable() function. Lua tables already support immediate lookup of entries in them, just by using square brackets. See the Lua documentation for tables.

So your code:

 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)

is really equivalent to:

if valid[22] then
 return valid[22]
elseif valid[13] then
 return valid[13]
elseif valid[25] then
 return valid[25]

Can we do better than this? Sure! Lua has the or operator which returns the first true value. (See documentation)

return valid[22] or valid[13] or valid[25]

Can we do even better? Definitely. Make up another table with the indexes in order, then iterate over it, returning the first true value.

local priority = {22, 13, 25}
local i
for i in priority do
 if valid[i] return valid[i]
end
answered May 8, 2015 at 14:25
\$\endgroup\$
4
  • \$\begingroup\$ I got a attempt to call table value error. when using your for loop. local priority = {22, 13, 25} and for i in priority do if valid[priority] then return valid[priority] end end \$\endgroup\$ Commented May 8, 2015 at 15:12
  • 1
    \$\begingroup\$ nevermind I could fix it. thanks a lot for i = 1, #priority, 1 do if valid[tostring(priority[i])] then return valid[tostring(priority[i])] end end \$\endgroup\$ Commented May 8, 2015 at 15:32
  • \$\begingroup\$ or just put the strings in the priority table anyway; local priority={"22","13","25"} \$\endgroup\$ Commented May 8, 2015 at 15:49
  • \$\begingroup\$ also see my fix; i put the wrong thing inside the square brackets \$\endgroup\$ Commented May 8, 2015 at 15:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.