├── README.md ├── main.lua └── print_r.lua /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robmiracle/print_r/c764743c705f22ca2ff614f35794367ad17333ed/README.md -------------------------------------------------------------------------------- /main.lua: -------------------------------------------------------------------------------- 1 | local print_r = require("print_r") 2 | 3 | local t = { hello = "world" } 4 | 5 | print_r(t) 6 | -------------------------------------------------------------------------------- /print_r.lua: -------------------------------------------------------------------------------- 1 | local function print_r ( t ) 2 | local print_r_cache={} 3 | local function sub_print_r(t,indent) 4 | if (print_r_cache[tostring(t)]) then 5 | print(indent.."*"..tostring(t)) 6 | else 7 | print_r_cache[tostring(t)]=true 8 | if (type(t)=="table") then 9 | local tLen = #t 10 | for i = 1, tLen do 11 | local val = t[i] 12 | if (type(val)=="table") then 13 | print(indent.."#["..i.."] => "..tostring(t).." {") 14 | sub_print_r(val,indent..string.rep(" ",string.len(i)+8)) 15 | print(indent..string.rep(" ",string.len(i)+6).."}") 16 | elseif (type(val)=="string") then 17 | print(indent.."#["..i..'] => "'..val..'"') 18 | else 19 | print(indent.."#["..i.."] => "..tostring(val)) 20 | end 21 | end 22 | for pos,val in pairs(t) do 23 | if type(pos) ~= "number" or math.floor(pos) ~= pos or (pos < 1 or pos > tLen) then 24 | if (type(val)=="table") then 25 | print(indent.."["..pos.."] => "..tostring(t).." {") 26 | sub_print_r(val,indent..string.rep(" ",string.len(pos)+8)) 27 | print(indent..string.rep(" ",string.len(pos)+6).."}") 28 | elseif (type(val)=="string") then 29 | print(indent.."["..pos..'] => "'..val..'"') 30 | else 31 | print(indent.."["..pos.."] => "..tostring(val)) 32 | end 33 | end 34 | end 35 | else 36 | print(indent..tostring(t)) 37 | end 38 | end 39 | end 40 | 41 | if (type(t)=="table") then 42 | print(tostring(t).." {") 43 | sub_print_r(t," ") 44 | print("}") 45 | else 46 | sub_print_r(t," ") 47 | end 48 | 49 | print() 50 | end 51 | --------------------------------------------------------------------------------