Skip to content

Instantly share code, notes, and snippets.

@ahappyforest
Created March 25, 2015 09:20
Show Gist options
  • Save ahappyforest/00afc1cd9324fd498835 to your computer and use it in GitHub Desktop.
Save ahappyforest/00afc1cd9324fd498835 to your computer and use it in GitHub Desktop.
print lua table as lua style
function print_r(data)
local cstring = "";
local function table_len(t)
local i = 0
for k, v in pairs(t) do
i = i + 1
end
return i
end
local function tableprint(data,cstring)
if data == nil then
local_print("core.print data is nil");
end
local cs = cstring .. " ";
local_print(cstring .."{");
if(type(data)=="table") then
for k, v in pairs(data) do
if type(v) ~= "table" then
local_print(cs..tostring(k).." = "..tostring(v));
elseif table_len(v) == 0 then
local_print(cs..tostring(k).." = ".."{}")
else
local_print(cs..tostring(k).." = ");
if(type(v)=="table") then
tableprint(v,cs);
end
end
end
else
local_print(cs..tostring(data));
end
local_print(cstring .."}");
end
tableprint(data,cstring);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment