Is there a better way to use if to check more than one thing....?
Like: I do:
if string == "taco" or string == "pizza" or string == "duck" then
better way:
if string == ('taco'or'duck') then
or something similar?
Is there a better way to use if to check more than one thing....?
Like: I do:
if string == "taco" or string == "pizza" or string == "duck" then
better way:
if string == ('taco'or'duck') then
or something similar?
There's not really a better way. However, if there are a ton of string you would like to check, you could loop through a table and return if the string matches any of the values stored in the table.
Example,
local strings = {
"taco";
"cheese";
"men";
"women";
"happy";
"sad";
"pyrondon";
"is";
"weird"
}
function check(string)
for i,v in pairs(strings) do
if string == v then
return true
end
end
return false
end
if check("men") then
print("Returns True")
end
That would only really be practical if you have tons of strings you want to check.
Good Luck!
Well dang. Thanks for this though, could probably use it if I have like 20 or more strings. Lol
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
You would use a switch/ case select but Lua does not have that functionality. http://lua-users.org/wiki/SwitchStatement