Check if array contains specific value

前端 未结 3 1354
春和景丽
春和景丽 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 09:54

    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] == true then
      print("message")
    else
      print("other message")
    end
    

提交回复
热议问题