1

been trying to get this one on my own for a few days but got out of ideas. Here's how it goes...

expect user input of 1 to x words into a local variable, every word has to be compared to a table with multiple strings, one at a time, if all words are true for one of the strings then do something (it will print other fields of that table ID to the user) or if one of words is missing do another thing (just tell the user the search returned no results) words have not to be matched completely.

1

1 Answer 1

2

hmmm... Sounds like a simple problem, you're probably over-thinking it or expecting there to be some very simple way of doing it, but (unless you want to use some tool like lpeg), there's really just the obvious way:

local function find_words_in_string(str, words)
   for i, word in ipairs(words) do -- Try every word
      if not str:find(word) then -- See if it's in the string
         return false -- Say no if it isn't
      end
   end
   return true
end

If you want to be fancy, you can also implement it as a recursive variadic function:

local function find_words_in_string(str, word, ...)
   if word then
      return str:match(word) and find_words_in_string(str, ...)
   else
      return true
   end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.