Lua 中 for 循环的值打印为 nil

看看 for 循环和其中的 word 部分。

local words = {
    "One - Test",
    "Two - Test",
    "Three - Test"
}

local find = "Test"

local function getWord(partialName)
    partialName = partialName:lower()
    for _,word in ipairs(words) do

        if string.find(word:lower(),partialName) then
            print(word)
        end
    end
end

getWord(find)
输出:

One - Test
Two - Test
Three - Test

我正在尝试将所有输出的内容存储到其他变量中。print(word) 输出了上面看到的内容,但我该如何只获取其中的 One - Test 并将其存储到另一个变量中呢?我尝试使用 print(word[1]) 进行测试,但它没有起作用并输出了 nil。

nil (x3)  -  Client - Local Script:14

现在我怎么修复它呢?非常感谢!

原文链接 https://stackoverflow.com/questions/69434788

点赞
stackoverflow用户2858170
stackoverflow用户2858170

将结果放入表格

local words = {
    "One - 测试",
    "Two - 测试",
    "Three - 测试"
}

local find = "测试"

local function getWord(partialName)
    partialName = partialName:lower()
    local output = {}
    for _,word in ipairs(words) do
        if string.find(word:lower(),partialName) then
            table.insert(output, word)
        end
    end
    return output
end

local result = getWord(find)
local tableContent = "| 匹配项 |\n|:---:|\n"
for _, word in ipairs(result) do
    tableContent = tableContent .. "| " .. word .. " |\n"
end

print(tableContent)
2021-10-04 12:43:04