所以这基本上就是我要处理的
datavalues={{VALUE=896},{VALUE=965},{VALUE=569}}
我确实需要嵌套表,因为稍后我将使用添加的时间戳修改每个嵌套表,在迭代后满足一些检查时,使用下面的行
datavalues[i].TIMESTAMP=something
然后嵌套的表将如下所示
datavalues={{VALUES=896, TIMESTAMP=596664},{VALUES=965, TIMESTAMP=597864},{VALUES=569, TIMESTAMP=597864}}
这是手动填充ofc,但我不想那样!我只想通过一些功能来填充。
我尝试过的天真的解决方案(当然它不起作用)
datavalues={}
function addtotable(id)
if #datavalues==0
then table.insert(datavalues,{VALUE=id})
end
if #datavalues>0
then for i=1,#datavalues do
if datavalues[i].VALUE==id
then break
else table.insert(datavalues,{VALUE=id})
end
end
end
end
想要的行为是:当我对同一个值(例如外接程序(595))使用该函数两次时,它只在第一次工作(只添加一次唯一值)。
当前的行为:它不工作,它对一个值有效,如果我添加第二个值并重新添加第一个值,它只是在另一个嵌套表中添加相同的值,而它不应该这样做。
这对我来说很重要的是,保持我之前发布的桌子的擦洗!
编辑:基本上,所发生的事情: addtotable(168)只运行一次或x段时间:
datavalues={{VALUE=168}}
这是预料中的
外接程序(168)运行一次,然后外接程序(169)运行多次。
datavalues={{VALUE=168},{VALUE=169},{VALUE=169},{VALUE=169},{VALUE=169},{VALUE=169},....} etc
我想要修复这种行为,并且只有
datavalues={{VALUE=168},{VALUE=169}} no matter how many times i run each
发布于 2022-07-16 19:00:38
在代码中标记的错误:
datavalues = { }
function addtotable(id)
if #datavalues==0
then table.insert(datavalues,{VALUE=id})
end
if #datavalues>0 then
for i=1,#datavalues do
if datavalues[i].VALUE==id then
break
else
--right here is your error. Each time an entry does not match, you add another entry.
table.insert(datavalues,{VALUE=id})
end
end
end
end
你可能想要这样的东西:
datavalues = { }
function addtotable(id)
for i=1,#datavalues do
if datavalues[i].VALUE==id then
return --don't execute anything else
end
end
--we can only end up here if return has never been called, thus no entry has been found
table.insert(datavalues,{VALUE=id})
end
https://stackoverflow.com/questions/73004465
复制相似问题