lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I haven't seen your message before. Thank you. I need to take a look more carefully to understand the usage of this library.
I am really impressed about the number of relevant modules are available.
Up to now, I only leaded with the standard libraries.

----- Original Message ----- From: "joao lobato" <>
To: "Lua list" <>
Sent: Sunday, March 07, 2010 6:12 PM
Subject: Re: Saving a table in file

This one is generalized to accept
tables as indices as well as values; the save function gives you a
string representation of your table that you can load using loadstring
or loadfile. I don't recall how much testing I did regarding function
environments but they're disabled by default.

On 3/7/10, Andre Leiradella <> wrote:
This version handles nested tables:



On 07/03/2010 17:25, Nevin Flanagan wrote:
One way to serialize tables in Lua is to write the actual table
constructor text into the file. This has the advantage that you can
then use Lua itself to load the table from the file contents.

This is a simplified version that does not handle nested tables:

function table.pickle(t, fileName)
local output =, "w")
output:write("return {\n")
for k, v in pairs(t) do
if (type(k) == 'string' or type(k) == 'number' or type(k) == 'boolean')
and (type(v) == 'string' or type(v) == 'number' or type(v) ==
'boolean'') then
if type(k) == 'string' then
k = string.format("%q", k)
k = tostring(k)
if type(v) == 'string' then
v = string.format("%q", v)
v = tostring(v)
output:write(string.format("[%s] = %s,\n", k, v)