开发者社区> 问答> 正文

lua table 去重?报错

lines = {
mac = "00:1b:c5:09:a0:58",
time = "10",
}


a = {
"00:1b:c5:09:a0:58",
"00:1b:c5:09:a0:55",
"00:1b:c5:09:a0:54",
}


for i=1,#a do
-- print("a[i]..is"..a[i])
-- print("lines['mac'] is"..lines["mac"])
if(a[i] == lines["mac"])then
table.remove(lines,lines["mac"]);
else
table.insert(a,lines["mac"])
end

end


for k,v in pairs(a) do
print(v)
end



报错:

bad argument #2 to 'remove' (number expected, got string)

<p>
	<span>lines["mac"]  有值为什么不行呢?</span>
</p>
<p>
	<span><br>


求大牛指点





展开
收起
爱吃鱼的程序员 2020-06-14 15:33:13 506 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    table.remove接受的是index

    另外你在循环里面直接table.insert这样真的好么?

    insert是为tablea添加新数据如何做可以在判断到相同的数据时删除lines["mac"]呢?

    insert是为tablea添加新数据  

    如何做可以在判断到相同的数据时删除lines["mac"]呢?

    lines["mac"]删除的话 lines['mac']=nil

    lines={mac="00:1b:c5:09:a0:58",time="10",}a={"00:1b:c5:09:a0:58","00:1b:c5:09:a0:55","00:1b:c5:09:a0:54",}localfunctiondump(table)fork,vinpairs(table)doprint(k)print(v)print("*********")endendfori=1,#ado--print("a[i]..is"..a[i])--print("lines['mac']is"..lines["mac"])if(a[i]==lines["mac"])then--table.remove(lines,i);lines['mac']=nilelsetable.insert(a,lines["mac"])endend--dump(lines)fork,vinpairs(a)doprint(v)end



    2020-06-14 15:33:31
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Lazy-Join Optimizations withou 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载