lua table 去重

流岚十七 发布于 2015/06/09 13:47
阅读 3K+
收藏 0
Lua
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)

lines["mac"]  有值为什么不行呢?



求大牛指点





加载中
0
鸢月
鸢月

table.remove接受的是index

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

流岚十七
流岚十七
insert是为table a 添加新数据 如何做可以在判断到相同的数据时删除lines["mac"] 呢?
0
流岚十七
流岚十七

insert是为table a 添加新数据   

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

0
mickelfeng
mickelfeng

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",
}

local function dump(table)
    for k, v in pairs( table ) do
        print(k)
        print(v)
        print("*********")
    end
end


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,i);
		lines['mac']=nil
	else 
		table.insert(a,lines["mac"])
	end
end

--dump(lines)

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



返回顶部
顶部