如果列表 lua 中的项目



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

hello
hello1
hello2

我想看看x字符串是否在我的列表中。 我知道如何在 python 中做到这一点,但我完全不知道如何在 lua 中做到这一点(有点紧凑?

在蟒蛇中:

list = ["hello1", "hello2", "hello3"]
if "hello1" in list:
print("this worked!")
else:
print("this didn't")

你会想做这样的事情:

local list = {"Example", "Test"} -- This is the list of items
local target = "Test" -- This is what it will look for
local hasFound =  false
for key, value in pairs(list) do -- For all the values/"items" in the list, do this:
if value == target then
hasFound = true
end
-- You dont want to add an else, since otherwise only the last item would count.
end
if hasFound then -- If you dont put anything then it automatically checks that it isnt either nil nor false.
print("Item "..target.." has been found in list!")
else
print("Item "..target.." has not been found in list!")
end

最新更新