在一个表变量中搜索特定字符集并返回键值

我正在尝试找到一种方法来获取包含特定字词的变量的表键

boxOfFrogs = {"hello how are you", "I am fine", "thank you"}

我找到了

  local function has_value (tab, val)
    for index, value in ipairs(tab) do
        if value == val then
            return index
        end
    end
    return false
end

但是它只适用于在表中搜索完整变量的情况,我无法运行以下代码

key = has_value (boxOfFrogs, "fine")

好吧,我知道我可以运行它,但是它会返回 "False" ,因为该表中没有仅包含单词 "fine" 的键/变量,我尝试在函数中使用 string.match(),但是它会出错,例如

print(has_value(boxOfFrogs, string.match("fine")))

会输出:

lua: [string ""]:12: bad argument #2 to 'match' (string expected, got no value) stack traceback: [C]: in function 'match' [string ""]:12: in main chunk

我该如何搜索包含单词 "fine" 的字段的键值?

点赞