在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
例子1 : 1 ableprint = function(data,cstring,deepIndex) --第二个参数可以为空,第三个参数不要手动添加,它是用来进行打印深度控制的。 2 if data == nil then 3 print("core.print data is nil"); 4 end 5 6 if deepIndex == nil then deepIndex = 1 end 7 if cstring == nil then cstring = "" end 8 9 local cs = cstring .. " "; 10 print(cstring .."{"); 11 if(type(data)=="table") then 12 for k, v in pairs(data) do 13 if(type(v) == "table") then print(cs..tostring(k).." = "); 14 else print(cs..tostring(k).." = "..tostring(v)); end 15 16 if(type(v)=="table") then 17 18 if (deepIndex + 1) < 5 then --5就是控制的打印深度(你可以根据需要调整),以防数据结构中存在循环饮用(别以为不可能,我就遇到过) 19 zr.tableprint(v,cs,(deepIndex+1)); 20 else 21 print(cs.." {"..tostring(v).."}"); 22 end 23 end 24 end 25 else 26 print(cs..tostring(data)); 27 end 28 print(cstring .."}"); 29 end
例子2: 1 function var_dump(data, max_level, prefix) 2 if type(prefix) ~= "string" then 3 prefix = "" 4 end 5 if type(data) ~= "table" then 6 print(prefix .. tostring(data)) 7 else 8 print(data) 9 if max_level ~= 0 then 10 local prefix_next = prefix .. " " 11 print(prefix .. "{") 12 for k,v in pairs(data) do 13 io.stdout:write(prefix_next .. k .. " = ") 14 if type(v) ~= "table" or (type(max_level) == "number" and max_level <= 1) then 15 print(v) 16 else 17 if max_level == nil then 18 var_dump(v, nil, prefix_next) 19 else 20 var_dump(v, max_level - 1, prefix_next) 21 end 22 end 23 end 24 print(prefix .. "}") 25 end 26 end 27 end
|
请发表评论