Check if array contains specific value

前端 未结 3 1412
春和景丽
春和景丽 2021-02-12 09:22

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\

3条回答
  •  耶瑟儿~
    2021-02-12 10:13

    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
    

提交回复
热议问题