Object 在lua中动态循环表
在下面的代码中,我尝试在pathstr和arr之间进行模式匹配 示例-根据pathstr“/sample/find/apples/red”,如果在arr表中找到red和apples,则我将设置 将值标记为true并中断循环 目前我已经在循环中硬编码了v[2]。如何避免这种情况并动态检查表中的值Object 在lua中动态循环表,object,lua,Object,Lua,在下面的代码中,我尝试在pathstr和arr之间进行模式匹配 示例-根据pathstr“/sample/find/apples/red”,如果在arr表中找到red和apples,则我将设置 将值标记为true并中断循环 目前我已经在循环中硬编码了v[2]。如何避免这种情况并动态检查表中的值 function ContainsWholeWord(input, word) return string.find(input, "%f[%w]" .. word .. "%f[%W]") en
function ContainsWholeWord(input, word)
return string.find(input, "%f[%w]" .. word .. "%f[%W]")
end
local pathstr = "/sample/find/fruits/Apple/red"
local flag = false
local arr =
{
fruits = {Apple={price=10,color=red}},
vegetables = { Beans={price=10,color=red}}
}
for k, v in pairs(arr) do
//call ContainsWholeWord function and match pattern of table in pathstr.
If found, set flag value
flag = true
print(flag)
break
end
end
以下是您可以尝试的键值:
local arr = {
apples = { code="a", color="red", amount=5 },
oranges = { code="o", color="orange", amount=12 },
pears = { code="p", color="green", amount=7 },
}
然后
ContainsWholeWord(pathstr, v.color)
您可以尝试一个键值:
local arr = {
apples = { code="a", color="red", amount=5 },
oranges = { code="o", color="orange", amount=12 },
pears = { code="p", color="green", amount=7 },
}
然后
ContainsWholeWord(pathstr, v.color)
对于示例代码,我不理解dynamic意味着什么,因为您显然需要访问代码中的特定索引v。您的意思是“red”将是一个从pathstr搜索的字符串表吗?也许我遗漏了什么。我很困惑,你能给我们一个更简单的例子来说明你的问题吗?这对我们来说会更容易,你会学到更多。@warspyking@Rochet2我需要检查arr表中是否有“水果/苹果/红色”字样。我现在明白了,我怎样才能做到这一点<代码>如果type(arr.fruits)=“table”和type(arr.fruits.apple)=“table”和fruits.apple.color==“red”,那么这在您的情况下有效吗?如果是这样的话,我会发布一个答案。对于示例代码,我不明白动态意味着什么,因为您显然需要访问代码中的特定索引v。您的意思是“red”将是一个从pathstr搜索的字符串表吗?也许我遗漏了什么。我很困惑,你能给我们一个更简单的例子来说明你的问题吗?这对我们来说会更容易,你会学到更多。@warspyking@Rochet2我需要检查arr表中是否有“水果/苹果/红色”字样。我现在明白了,我怎样才能做到这一点<代码>如果type(arr.fruits)=“table”和type(arr.fruits.apple)=“table”和fruits.apple.color==“red”,那么这在您的情况下有效吗?如果是这样的话,我会发帖回答。