lua调试打印table算法

来源:互联网 发布:杭州 夜场小姐数据 编辑:程序博客网 时间:2024/06/02 17:27
function look(Obj)if type(Obj) ~= "table" thenprint(Obj)returnendlocal function Save(Obj, Level)local Blank = ""for i = 1, Level doBlank = Blank .. "   "endfor k,v in pairs(Obj) doif tostring(k) ~= "" and v ~= Obj thenif type(v) ~= "table" thenprint(Blank.. " [".. tostring(k).. "] = "..tostring(v))elseprint(Blank.. " [".. tostring(k).. "] = {")Save(v, Level + 2)print(Blank.."     },")endendendendprint("   {")Save(Obj, 1)print("   }")end



效果:

local tb_test={1,2,3,8,['ww']={11,33,{555,['num']=666,777}}}

循环生成缩进这部分可用 string.rep,应该能增强性能,对于调试影响不大,暂时这样用;另外想把传入打印的变量名打出来,如上面例子 tb_test={...},还没想到办法

0 0