TablePrinter.lua 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. -- Print anything - including nested tables
  2. -- Based on but modified from:
  3. -- http://lua-users.org/wiki/TableSerialization
  4. module("TablePrinter", package.seeall)
  5. function TablePrinter.print (tt, indent, done)
  6. done = done or {}
  7. indent = indent or 0
  8. if tt == nil then
  9. print("nil\n")
  10. else
  11. if type(tt) == "table" then
  12. for key, value in pairs (tt) do
  13. print(string.rep (" ", indent)) -- indent it
  14. if type (value) == "table" and not done [value] then
  15. done [value] = true
  16. print(string.format("[%s] => table\n", tostring (key)));
  17. print(string.rep (" ", indent+4)) -- indent it
  18. print("(\n");
  19. table_print (value, indent + 7, done)
  20. print(string.rep (" ", indent+4)) -- indent it
  21. print(")\n");
  22. else
  23. print(string.format("[%s] => %s\n",
  24. tostring (key), tostring(value)))
  25. end
  26. end
  27. else
  28. print(tt .. "\n")
  29. end
  30. end
  31. end