I'm trying to check if there are certain words in a string. So far I created a function that stores in the table test if the word is present in the string, then the function prints a message if the word is in the string and other message otherwise.
Here's the MWE:
stg = "In this string the words sky, dog, frog can be found"
function highlight(str)
local test = {str:find("sky"),str:find("car"),str:find("glass")}
local start, fim
for k, v in ipairs(test) do
if v ~= nil then
print("There's something")
elseif v == nil then
print("There's nothing")
end
end
end
highlight(stg)
The weird thing is: the function only recognize the first word that is being checking, that is, the word sky. If the stg string has none of the matching words, the function returns nothing. Not even the message There's nothing.
How to make the function check the if the words are present or missing in the string and print the messages correctly?
test = {1,nil,nil}, butnilis equivalent to erasing the element, i.e. it is as if removed from the table.stg = "In this string the words glass, dog, frog can be found"the function can't find glass. Why? Shouldn't I havetest = {nil, nil, 1}? If so, then the conditional would accuse that there's something.