local t ={{1,2,3},{{4,5,6},['a']='a'},{[123]=123,['123']='123'},{['a']=1,{['b']=2,{['c']=3}}}}
myModule.my_print_table_recursive(t,"想要打印的信息")
例子2:打印指定字段
local t ={{['name']="zhangSan",['age']=7,['gender']="male"},{['name']="liSi",['age']=8,['gender']="female"},{['name']="wangWu",['age']=9,['gender']="male"}}
myModule.my_print_table_recursive(t,"想要打印的信息",{_in={'name','age'}})
local myModule ={}function myModule.tablefind(tbl, val)if tbl ==nilor val ==nilortype(tbl)~="table"thenreturnnilendfor i, v inpairs(tbl)doif v == val thenreturn i
endendreturnnilend-- getRealValue 如果val为string类型则给他加上双引号,便于更好识别当key为数字时两种不同类型number和string的区别function myModule.getRealValue(val)iftype(val)=="string"andnot string.find(val,"\"")then
val ="\""..val.."\""endreturn val
end---my_print_table_recursive---@param t table---@param str string 需要打印出来的提示信息---@param fileds table={_in={},_out={}} _in需要打印的字段, _out不需要打印的字段function myModule.my_print_table_recursive( t, str, fileds)local print_r_cache={}localfunctionsub_print_r(t,indent)if(print_r_cache[tostring(t)])thenprint(indent.."*"..tostring(t))else
print_r_cache[tostring(t)]=trueif(type(t)=="table")thenfor pos,val inpairs(t)doif fileds and fileds._out and myModule.tablefind(fileds._out, pos)thengoto continue
endif fileds and fileds._in andnot myModule.tablefind(fileds._in, pos)thengoto continue
endif(type(val)=="table")thenprint(indent.."["..myModule.getRealValue(pos).."] => "..tostring(t).." {")sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))print(indent..string.rep(" ",string.len(pos)+6).."}")elseif(type(val)=="string")thenprint(indent.."["..myModule.getRealValue(pos)..'] => "'..val..'"')elseprint(indent.."["..myModule.getRealValue(pos).."] => "..tostring(val))end::continue::endelseprint(indent..tostring(t))endendendprint("\n***** print begin ********* ", str)if(type(t)=="table")thenprint(tostring(t).." {")sub_print_r(t," ")print("}")elsesub_print_r(t," ")endprint("***** print end ********* ", str,"\n")end---my_print_table_normal---@param ta table---@param str string 需要打印出来的提示信息---@param fileds table={_in={},_out={}} _in需要打印的字段, _out不需要打印的字段function myModule.my_print_table_normal(ta, str, fileds)print("\n***** print begin ********* ", str)iftype(ta)=="table"thenfor k,v inpairs(ta)doif fileds and fileds._out and myModule.tablefind(fileds._out, k)thengoto continue
endif fileds and fileds._in andnot myModule.tablefind(fileds._in, k)thengoto continue
endprint("key= "..myModule.getRealValue(k),"value= ",myModule.getRealValue(v))::continue::endendprint("***** print end ********* ", str,"\n")endreturn myModule
local myModule = {}function myModule.tablefind(tbl, val) if tbl == nil or val == nil or type(tbl) ~= "table" then return nil end for i, v in pairs(tbl) do if v == val then return i end end return