Lua Sets Search for an item in a list


Example

There's no built in way to search a list for a particular item. However Programming in Lua shows how you might build a set that can help:

function Set (list)
  local set = {}
  for _, l in ipairs(list) do set[l] = true end
  return set
end

Then you can put your list in the Set and test for membership:

local items = Set { "apple", "orange", "pear", "banana" }

if items["orange"] then
  -- do something
end