You have a few options here.
One, is to create a set:
local set = {
foo = true,
bar = true,
baz = true
}
Then you check if either of these are in the table:
if set.bar then
The drawback to this approach is that you won't iterate over it in any specific order (pairs returns items in an arbitrary order).
Another option would be to use a function to check each value in a table. This'll be very slow in large tables, which brings us to back to a modification of the first option: A reverse lookup generator: (This is what I'd recommend doing -- unless your set is static)
local data = {"milk", "butter", "cheese"}
local function reverse(tbl, target)
local target = target or {}
for k, v in pairs(tbl) do
target[v] = k
end
return target
end
local revdata = reverse(data)
print(revdata.cheese, revdata.butter, revdata.milk)
-- Output: 3 2 1
This'll generate a set (with the added bonus of giving you the index where the value was in your original table). You can also put the reverse into the same table as the data was in, but this won't go well with numbers (and it'll be messy if you need to generate the reverse again).
hasValue? What ismilk, without the quotation marks by the way? As lua is a dynamic language, no one should be too eager to answer a question fully, where many things are not defined. - Dmitry Ledentsovtableis a standard library. It's best to treat such globals as reserved variables. Once you runtable =, you'll no longer have access to thetablestandard library. That's why @Aedil has used items. - Tom Blodgetfor key, value in pairs(math) do print(string.format("(%s)%q ⇒ (%s)%q", type(key), tostring(key), type(value), tostring(value))) endon whatever table you're interested. That was for the math library's table. - Tom Blodgetk, vstructure to my table? Like, i already tried it once, it returns fail even with those entries which are fed in table. - user3762712