Does Lua have any statement equivalent to the "in" statement of python?
Example:
if "word" in variable:
Does Lua have any statement equivalent to the "in" statement of python?
Example:
if "word" in variable:
Lua has no equivalent of Python's in
operator, but if you know what container type you want to emulate, it's easy to write a function for it.
str
The true
argument ensures that the substring is treated literally and not as a pattern. See string.find
for details.
local function inString(s, substring)
return s:find(substring, 1, true)
end
list
local function inArray(array, x)
for _, v in ipairs(array) do
if v == x then
return true
end
end
return false
end
dict
local function inKeys(t, k)
return t[k] ~= nil
end