0

So I have a list in lua which looks something like this

hello
hello1
hello2

and I want to see if x string is in my list. I know how to do this in python but I have absolutely no idea how to do it in lua (somewhat compactly?)

In python:

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

if "hello1" in list:
   print("this worked!")
else:
   print("this didn't")
popsmoke
  • 75
  • 1
  • 1
  • 6

1 Answers1

2

You'd want to do something like this:

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