print_r.lua 790 B

12345678910111213141516171819202122232425262728293031
  1. local print = print
  2. local tconcat = table.concat
  3. local tinsert = table.insert
  4. local srep = string.rep
  5. local type = type
  6. local pairs = pairs
  7. local tostring = tostring
  8. local next = next
  9. local function print_r(root)
  10. local cache = { [root] = "." }
  11. local function _dump(t,space,name)
  12. local temp = {}
  13. for k,v in pairs(t) do
  14. local key = tostring(k)
  15. if cache[v] then
  16. tinsert(temp,"+" .. key .. " {" .. cache[v].."}")
  17. elseif type(v) == "table" then
  18. local new_key = name .. "." .. key
  19. cache[v] = new_key
  20. tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key))
  21. else
  22. tinsert(temp,"+" .. key .. " [" .. tostring(v).."]")
  23. end
  24. end
  25. return tconcat(temp,"\n"..space)
  26. end
  27. print(_dump(root, "",""))
  28. end
  29. return print_r