lua中table判空和插入table

首先是判空

testtable={}
if next(testtable) ==nil then 
    print("这个table是空的")
end

插入table

local table1={["name"]="aaaa",["value"]=1}

local table3={["name"]="cccc",["value"]=3,["my"]={}}
local table4={["name"]="dddd",["value"]=4,["my"]={}}

-- table.insert(table3.my,table1) 修改table3中table1中的值,table4中的table1也会发生改变
-- table.insert(table4.my,table1)

-- table3.my[1]=table1; 修改table3中table1中的值,table4中的table1也会发生改变
-- table4.my[1]=table1;

table3.my[1]={["name"]="aaaa",["value"]=1}--这种方法table3和table4互不干扰

table4.my[1]={["name"]="aaaa",["value"]=1}

table3.my[1].value=100

print(string.format("table3:%s
table4:%s",json.encode(table3),json.encode(table4)))
原文地址:https://www.cnblogs.com/still-smile/p/13299730.html