lua 除了简单类型分配内存外,table只是传递引用,所以不能用简单的"="来copy两个表,并试图修改一个表中的值。
下面方法实现table
copy:
--lua table 拷贝
function table_copy_table(ori_tab)
if (type(ori_tab) ~= "table") then
return nil
end
local new_tab = {}
for i,v in pairs(ori_tab) do
local vtyp = type(v)
if (vtyp == "table") then
new_tab[i] = table_copy_table(v)
elseif (vtyp == "thread") then
new_tab[i] = v
elseif (vtyp == "userdata") then
new_tab[i] = v
else
new_tab[i] = v
end
end
return new_tab
end