Return table value based on a primitive "priority queue"
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