ajmurmann
ajmurmann

Reputation: 1635

Human readable string representation of table in Lua

I am new to Lua and want to print the contents of a table for debugging purposes. I can do that by iterating over the table myself. However, since this strikes me as a very common problem, I expect there must be an out of the box way of doing that or someone must have written a nice library that does that. WHat's the standard way of doing this in Lua?

Upvotes: 7

Views: 1660

Answers (3)

jpjacobs
jpjacobs

Reputation: 9549

This is an instance of the general problem of table serialization.

Take a look at the Table Serialization page at lua-users for some serious implementations.

My throw at it is usually quickly defining a function like

function lt(t) for k,v in pairs(t) do print(k,v) end end

Upvotes: 3

Yuval Rimar
Yuval Rimar

Reputation: 1055

See table.print in https://github.com/rimar/lua-reactor-light/blob/master/util.lua it was probably borrowed from lualogging library

Upvotes: 1

Corbin March
Corbin March

Reputation: 25714

For better or worse, there's no standard. Lua is known for what it excludes as much as for what it includes. It doesn't make assumptions about proper string representations because there's no one true way to handle things like formats, nested tables, function representation, or table cycles. That being said, it doesn't hurt to start with a "batteries-included" Lua library. Maybe consider Penlight. Its pl.pretty.write does the trick.

Upvotes: 4

Related Questions