-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprint_r.lua
50 lines (48 loc) · 1.91 KB
/
print_r.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
local function print_r ( t )
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
local tLen = #t
for i = 1, tLen do
local val = t[i]
if (type(val)=="table") then
print(indent.."#["..i.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(i)+8))
print(indent..string.rep(" ",string.len(i)+6).."}")
elseif (type(val)=="string") then
print(indent.."#["..i..'] => "'..val..'"')
else
print(indent.."#["..i.."] => "..tostring(val))
end
end
for pos,val in pairs(t) do
if type(pos) ~= "number" or math.floor(pos) ~= pos or (pos < 1 or pos > tLen) then
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end