## Snippet for dumping tables when testing

pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

### Snippet for dumping tables when testing

Hi guys and girls,

I was having difficulty testing my apps without being able to inspect the contents of a table by printing it on the screen like var_dump does in PHP. I pulled my sleeves, poured a cup of coffee, and coded this little snippet.

If you have any suggestions for improvements, that would be most appreciated.

All lua types are supported except functions and userData. If someone knows how to support functions by writing them out as strings, that would be great. I'm not even sure if it's possible.

The main function is below. You can include it in your code however you like. Make it a local function if you can.

Code: Select all

--[[
Dumps a table for inspection

@param  table   table to inspect
@param  mixed   table name [def: 1]
@param  number  x starting position [def: 10]
@param  number  y starting position [def: 10]
@param  number  tab width [def: 20]
@param  number  newLine height [def: 20]
@param  mixed   number of newLines at EOL [def: 2]
@return numbers x ending position, y ending position
--]]
function dumpTable(t, k, x, y, tab, NL, numNLatEOF)
k = k or 1
x = x or 10
y = y or 10
tab = tab or 20
NL = NL or 20
if numNLatEOF == nil then numNLatEOF = 2 end
if type(k) == "number" then k = "[" .. k .. "]" end
love.graphics.print(k .. " = {", x, y)
x = x + tab
y = y + NL
for k,v in pairs(t) do
if type(v) == "table" then
x, y = dumpTable(v, k, x, y, tab, NL, false)
else
if v == true then v = "true"
elseif v == false then v = "false"
elseif type(v) == "string" then v = '"' .. v .. '"'
elseif type(v) == "function" then v = "function"
elseif type(v) == "userdata" then v = "userdata"
end
if type(k) == "number" then k = "[" .. k .. "]" end
love.graphics.print(k .. ' = ' .. v, x, y)
end
y = y + NL
end
x = x - tab
love.graphics.print("}", x, y)
if numNLatEOF then y = y + NL * numNLatEOF end
return x, y
end

You can call the function inside love draw. Here are some pseudo code examples. There are no examples showing the modification of x, y, tab, or newLine, but I'm sure you can figure that out.

Code: Select all

-- Ex: Dumping one table
function love.draw()
dumpTable(table)
end

-- Ex: Dumping one table with a name
function love.draw()
dumpTable(table, "tableName")
end

-- Ex: Dumping many tables
function love.draw()
local x, y = dumpTable(table, "tableName")
x, y = dumpTable(table, "tableName", x, y)
x, y = dumpTable(table, "tableName", x, y)
dumpTable(table, "tableName", x, y)
end

I hope that helps somebody. I'm sure finding it useful.
Last edited by pancakepalace on Wed Sep 21, 2011 9:35 pm, edited 6 times in total.

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Snippet for dumping tables when testing

pancakepalace wrote:If someone knows how to support functions by writing them out as strings, that would be great.
string.dump

pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

### Re: Snippet for dumping tables when testing

Unfortunately string.dump changes the function into binary code which is not really human readable. What would have been great would have been to change this:

Code: Select all

ThisIsAFunction = function(x) return x end

into this:

Code: Select all

ThisIsAHumanReadableString = "function(x) return x end"

NOTE: Iv'e modified my snippet to display 'function' and 'userdata' when either type is encountered. The version I had previously posted reported an error for those types.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Snippet for dumping tables when testing

pancakepalace wrote:Unfortunately string.dump changes the function into binary code which is not really human readable.
Well, if you want to do that, you need to include a disassembler. Unfortunately, there are not that many for Lua 5.1, and they output human readable Lua byte code, and not Lua.

pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

### Re: Snippet for dumping tables when testing

Thanks for the heads up Robin. This is just a quick function for debugging, so I won't go that far! I'd rather spend time making games to debug than diving in to a full fledge debugger!

NOTE: Corrected a bug in the snippet in case you downloaded it early than this post.

slime
Solid Snayke
Posts: 2883
Joined: Mon Aug 23, 2010 6:45 am
Contact:

### Re: Snippet for dumping tables when testing

Robin wrote:
pancakepalace wrote:Unfortunately string.dump changes the function into binary code which is not really human readable.
Well, if you want to do that, you need to include a disassembler. Unfortunately, there are not that many for Lua 5.1, and they output human readable Lua byte code, and not Lua.

http://forum.xda-developers.com/showpos ... stcount=49

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Snippet for dumping tables when testing

I am aware of LuaDec51, but it seemed like it couldn't do much more than I described earlier. If this is not the case, please tell me.
I have not seen this, but aa bunch of exes hardly seems appropriate for a LÖVE projects.

slime
Solid Snayke
Posts: 2883
Joined: Mon Aug 23, 2010 6:45 am
Contact:

### Re: Snippet for dumping tables when testing

Robin wrote:
I am aware of LuaDec51, but it seemed like it couldn't do much more than I described earlier. If this is not the case, please tell me.
I have not seen this, but aa bunch of exes hardly seems appropriate for a LÖVE projects.
You'll get ~90% complete code by using one of those (depending on if the code uses certain aspects of Lua or not).

pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

### Re: Snippet for dumping tables when testing

That sure seems like overkill to add a simple feature to a very simple function used only in debugging.

Looking at my function, I'm wondering if I really need those 'local' keywords at the beginning to make the variables local. I remember reading that function arguments were always local, except for tables that get overwritten. This is an aspect of Lua that always confuses me. The problem is I saw many code examples using the 'local' keyword in this instance when setting default values for the function arguments. Somehow though, I think this is superfluous. Just not sure.

Code: Select all

function dumpTable(t, k, x, y, tab, NL, numNLatEOF)
local k = k or 1
local x = x or 10
local y = y or 10
local tab = tab or 20
local NL = NL or 20
etc...
end


kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

### Re: Snippet for dumping tables when testing

pancakepalace wrote:That sure seems like overkill to add a simple feature to a very simple function used only in debugging.

Looking at my function, I'm wondering if I really need those 'local' keywords at the beginning to make the variables local. I remember reading that function arguments were always local, except for tables that get overwritten. This is an aspect of Lua that always confuses me. The problem is I saw many code examples using the 'local' keyword in this instance when setting default values for the function arguments. Somehow though, I think this is superfluous. Just not sure.

Code: Select all

function dumpTable(t, k, x, y, tab, NL, numNLatEOF)
local k = k or 1
local x = x or 10
local y = y or 10
local tab = tab or 20
local NL = NL or 20
etc...
end

yeh local here is redundant.

### Who is online

Users browsing this forum: No registered users and 34 guests