如果 item 在 list 中的 lua

所以我有一个 lua 中的列表,看起来像这样

hello
hello1
hello2

我想查看 x 字符串是否在我的列表中。 我知道如何在 Python 中实现,但我绝对不知道如何在 lua 中实现(比较紧凑的方式?)

在 Python 中:

list = ["hello1", "hello2", "hello3"]

if "hello1" in list:
   print("this worked!")
else:
   print("this didn't")
点赞
用户88888888
用户88888888

你想要做这样的事情:

local list = {"Example", "Test"} -- 这是物品列表
local target = "Test" -- 这是要查找的
local hasFound =  false

for key, value in pairs(list) do -- 对于列表中的所有值/"物品",执行以下操作:
    if value == target then
        hasFound = true
    end
    -- 你不想加else,因为这样只有最后一个项目才会计数。
end

if hasFound then --如果没有放任何东西,则自动检查它既不是nil也不是false。
    print("在列表中找到了物品"..target.."!")
else
    print("在列表中未找到物品"..target.."!")
end
2020-06-12 01:22:38