如果项在Lua数组中。

如何检查一个单词是否不在数组中......例如:

fruits = { 'apple', 'banana' }
value = "carrot"
if not value == fruits then
  print ( value .. " is not a fruit" )
end

或类似的东西?我倾向于纯 Lua。

点赞
用户1009479
用户1009479

直接方法:

local found = false
for _, v in ipairs(fruits) do
  if v == value then
    found = true
    break
  end
end

if not found then
  print ( value .. " 不是水果" )
end
2015-05-09 17:19:10