I have a string which is a space-seperated sequence of codes such as "O R LE Pdc"
. The code needs to check if a code matches any object in a table (such as {"R", "BE", "Cre"}
) return true, if not, return false. Here, it would return true because "R" exists.
I am completely stumped and do not know how to solve this problem.
CodePudding user response:
-- the table you're searching through
local t = {"R", "BE", "Cre"}
-- the text you're searching for
local txt = "O R LE Pdc"
-- now create a look-up table
local lut = {}
for _,v in ipairs(t) do
lut[v] = true
end
-- for every "word" in your string, check if you find it in the look up table
for str in txt:gmatch("%w ") do
if lut[str] then print("found it") end
end
Alternatively:
for _,v in ipairs(t) do
if txt:find(v) then print("found it") end
end
Or
for _, v in ipairs(t) do
for w in txt:gsub("%w ") do
if v == w then print("found it") end
end
end
There are many other ways. Which one to choose will depend on the amount of data you're processing.