Check if array contains specific value
Asked Answered
U

3

44

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?

Unstrap answered 3/11, 2015 at 23:2 Comment(1)
Did you intend for op to be an array-like table containing array-like tables with numbers at each of their first indices, or are you looking for op = {19, 18, 17}?Highpriced
H
58

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.

Highpriced answered 3/11, 2015 at 23:37 Comment(0)
C
28

You could also make the checking if the value exists in your array more efficient by moving your values to the index and assign them the true value.

Then when you check your table you just check if a value exists on that index, which will save you some time because you do not need to go through the whole table in the worst case scenario...

Here is the example I had in mind:

local op = {
[19]=true,
[18]=true,
[17]=true
}


if op[19] then
  print("message")
else
  print("other message")
end
Cuyler answered 9/11, 2015 at 10:4 Comment(1)
No need for the op[19] == true. That can just be op[19]Blandishments
I
10

The table op of your question is actually an array (table) of arrays.

To check whether a value exists in a table:

local function contains(table, val)
   for i=1,#table do
      if table[i] == val then 
         return true
      end
   end
   return false
end

local table = {1, 2, 3}
if contains(table, 3) then
   print("Value found")
end
Inapt answered 3/11, 2015 at 23:35 Comment(2)
That works only if the table is a sequence, which it is in this case. If it might not be, use something like for k,v in pairs(table) do if k == val then return true end return falseImplausible
Right. I think your comment requires further explanation, specially for those who are new to Lua. In Lua a table can be either a hash-table (stored as a sparse array) or an array (sequence). A sparse array contains nils. The length of an array is calculated as the number of elements until the first nil is found. That means it's not possible to iterate an array that contains nils with an index or ipairs. In that case, it is necessary to use pairs. That said, if you know your table is an array, it's better to use ipairs, as pairs is much slower.Inapt

© 2022 - 2024 — McMap. All rights reserved.