列表lua中的If项

列表lua中的If项,lua,Lua,我在lua有一个列表,看起来像这样 hello hello1 hello2 我想看看x字符串是否在我的列表中。 我知道如何在python中做到这一点,但我完全不知道如何在lua中做到这一点(有点紧凑?) 在python中: list=[“hello1”、“hello2”、“hello3”] 如果列表中有“hello1”: 打印(“这成功了!”) 其他: 打印(“这没有”) 您可能希望执行以下操作: local list = {"Example", "Test"} -- This is the

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

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

在python中:

list=[“hello1”、“hello2”、“hello3”]
如果列表中有“hello1”:
打印(“这成功了!”)
其他:
打印(“这没有”)

您可能希望执行以下操作:

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

这回答了你的问题吗?还是这个?