Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if array contains specific value

Tags:

lua

lua-table

I have this array, with some values (int) and I want to check if a value given by the user is equal to a value in that string. If it is, output a message like "Got your string".

Example of the list:

local op = { {19}, {18}, {17} }  if 13 == (the values from that array) then   message else   other message 

How can this be done?

like image 429
Ether Metin Avatar asked Nov 03 '15 23:11

Ether Metin


People also ask

How do you check if an array has a specific value in JavaScript?

For primitive values, use the array. includes() method to check if an array contains a value. For objects, use the isEqual() helper function to compare objects and array. some() method to check if the array contains the object.

How do you see if an array contains a value in Java?

We can use Arrays class to get the list representation of the array. Then use the contains() method to check if the array contains the value.

How do you check if an array does not contain a value JavaScript?

To check if an array doesn't include a value, use the logical NOT (!) operator to negate the call to the includes() method. The NOT (!) operator returns false when called on a true value and vice versa.


1 Answers

Lua doesn't have strict arrays like other languages - it only has hash tables. Tables in Lua are considered array-like when their indices are numerical and densely packed, leaving no gaps. The indices in the following table would be 1, 2, 3, 4.

local t = {'a', 'b', 'c', 'd'} 

When you have an array-like table, you can check if it contains a certain value by looping through the table. You can use a for..in loop, and the ipairs function to create a generic function.

local function has_value (tab, val)     for index, value in ipairs(tab) do         if value == val then             return true         end     end      return false end 

We can use the above in an if conditional to get our result.

if has_value(arr, 'b') then     print 'Yep' else     print 'Nope' end 

To reiterate my comment above, your current example code is not an array-like table of numbers. Instead it is an array-like table containing array-like tables, who have numbers in each of their first indices. You'd need to modify the function above to work with your shown code, making it less generic.

local function has_value (tab, val)     for index, value in ipairs(tab) do         -- We grab the first index of our sub-table instead         if value[1] == val then             return true         end     end      return false end 

Lua is not a very large or complex language, and its syntax is very clearcut. If the above concepts are totally alien to you, you'll need to spend some time reading real literature, not just copying examples. I would advise reading Programming in Lua to make sure you understand the very basics. This is the first edition, aimed at Lua 5.1.

like image 61
Oka Avatar answered Oct 14 '22 12:10

Oka