递归函数:ptable - 图2

万能打印 ptable(...)免费

格式 : string = ptable( 任何类型 )
适用场景 : 二值化/日常打印/替代print()

  1. function ptable(...)
  2. local function pta(t,L)
  3. local src,o="",""
  4. local L = L or 0 --[[记录递归次数]]
  5. local Tab = "\n"..string.rep("\t",L-1) --[[tab空格]]
  6. -- local Tab = "" --[[二值化打印]]
  7. local i = 0 --[[记录for循环次数]]
  8. for k,v in pairs(t) do
  9. if i > 0 then o = "," end
  10. i = i + 1
  11. local key,var="",""
  12. if type(k) == "string" then key = "[\'"..k.."\'] = " else key = "["..k.."] = " end
  13. --[[显示["key"],注释可隐藏]]
  14. if L == 0 then key = "" else key = src .. o .. Tab .. key end
  15. --[[初始值不显示Key]]
  16. if type(v) == "table" then
  17. src = key .. "{" ..pta(v,L+1).. Tab .."}"
  18. else
  19. if type(v) == "string" then
  20. var = "'"..v.."'"
  21. elseif type(v) == "number" then
  22. var = v
  23. elseif type(v) == "function" then
  24. var = "function"
  25. elseif type(v) == "boolean" then
  26. if v then var = "true" else var = "false" end
  27. end
  28. src = key .. var
  29. end
  30. if L == 0 then
  31. print(src) --[[输出]]
  32. end
  33. end
  34. return src
  35. end
  36. return pta({...}) --[[运行]]
  37. end
  1. --[[数据测试]]
  2. local t = {
  3. {2,a=true,3},
  4. {
  5. {"nihao"},
  6. {
  7. {},
  8. {}
  9. },
  10. {
  11. 3,
  12. {111},
  13. {222}
  14. }
  15. },
  16. 3
  17. }
  18. ptable( t, {11111111111111111}, {"你好"})
  19. --[[支持多表同时打印,支持打印"boolean"值,支持打印二值化]]

二值化打印方法:

递归函数:ptable - 图3
递归函数:ptable - 图4

递归函数:ptable - 图5这么好用,你猜我会告诉你吗???