dumptable.lua
 author Ryan C. Gordon Sun, 18 Jun 2017 19:40:30 -0400 changeset 56 a573346e6f7b parent 13 7b82873f8f94 permissions -rw-r--r--
 icculus@13 ` 1` ```function dumptable(tabname, tab, depth) ``` icculus@13 ` 2` ``` if depth == nil then -- first call, before any recursion? ``` icculus@13 ` 3` ``` depth = 1 ``` icculus@13 ` 4` ``` end ``` icculus@13 ` 5` icculus@13 ` 6` ``` if tabname ~= nil then ``` icculus@13 ` 7` ``` if tab == nil then ``` icculus@13 ` 8` ``` print(tabname .. " = nil") ``` icculus@13 ` 9` ``` return ``` icculus@13 ` 10` ``` else ``` icculus@13 ` 11` ``` print(tabname .. " = {") ``` icculus@13 ` 12` ``` end ``` icculus@13 ` 13` ``` end ``` icculus@13 ` 14` icculus@13 ` 15` ``` local depthstr = "" ``` icculus@13 ` 16` ``` for i=1,(depth*4) do ``` icculus@13 ` 17` ``` depthstr = depthstr .. " " ``` icculus@13 ` 18` ``` end ``` icculus@13 ` 19` icculus@13 ` 20` ``` if tab.DUMPTABLE_ITERATED then ``` icculus@13 ` 21` ``` print(depthstr .. "(...circular reference...)") ``` icculus@13 ` 22` ``` else ``` icculus@13 ` 23` ``` tab.DUMPTABLE_ITERATED = true ``` icculus@13 ` 24` ``` for k,v in pairs(tab) do ``` icculus@13 ` 25` ``` if type(v) == "table" then ``` icculus@13 ` 26` ``` print(depthstr .. tostring(k) .. " = {") ``` icculus@13 ` 27` ``` dumptable(nil, v, depth + 1) ``` icculus@13 ` 28` ``` print(depthstr .. "}") ``` icculus@13 ` 29` ``` else ``` icculus@13 ` 30` ``` if k ~= "DUMPTABLE_ITERATED" then ``` icculus@13 ` 31` ``` print(depthstr .. tostring(k) .. " = " .. tostring(v)) ``` icculus@13 ` 32` ``` end ``` icculus@13 ` 33` ``` end ``` icculus@13 ` 34` ``` end ``` icculus@13 ` 35` ``` tab.DUMPTABLE_ITERATED = nil ``` icculus@13 ` 36` ``` end ``` icculus@13 ` 37` icculus@13 ` 38` ``` if tabname ~= nil then ``` icculus@13 ` 39` ``` print("}") ``` icculus@13 ` 40` ``` end ``` icculus@13 ` 41` ```end ``` icculus@13 ` 42`