1
votes

I have a table something like this:

table = {milk, butter, cheese} -- without "Quotation marks"

I was searching for a way to check if a given value is in the table or not, and found this:

if table.hasValue(table, milk) == true then ...

but it returns nil, any reason why? (it says .hasValue is invalid) or can I get an alternative to check if value exists in that table? I tried several ways like:

if table.milk == true then ...
if table[milk] == true then ...

All of these returns nil or false.

4
have you read the 'programming in lua' book or looked up in the reference? It'd answer all your questions automatically. What gives you the reason to think that there's such a function hasValue? What is milk, 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 Ledentsov
The global table is a standard library. It's best to treat such globals as reserved variables. Once you run table =, you'll no longer have access to the table standard library. That's why @Aedil has used items. - Tom Blodget
Well @Dimitry I'm sorry I just asked because Google showed up hasValue in several searches. And thanks a lot guys its really appreciated. - user3762712
@user3762712 A table only has key-value pairs that you put in it. Try for key, value in pairs(math) do print(string.format("(%s)%q ⇒ (%s)%q", type(key), tostring(key), type(value), tostring(value))) end on whatever table you're interested. That was for the math library's table. - Tom Blodget
@TomBlodget an i get more details about how can i use k, v structure to my table? Like, i already tried it once, it returns fail even with those entries which are fed in table. - user3762712

4 Answers

1
votes

you can try this

items = {milk=true, butter=true, cheese=true}


if items.milk then
  ...
end

OR

if items.butter == true then
  ...
end

enter image description here

1
votes

A Lua table can act as an array or as an associative array (map).

There is no hasValue, but by using a table as an associative array you can easily implement it efficiently:

local table = {
   milk = true,
   butter = true,
   cheese = true,
}

-- has milk?
if table.milk then
   print("Has milk!")
end

if table.rocks then
   print("Has rocks!")
end
1
votes

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).

0
votes

If you write table = {milk=true, butter=true, cheese=true}, then you can use if table.milk == true then ....