[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Saving a table in file
- From: "Luciano de Souza" <luchyanus@...>
- Date: Sun, 7 Mar 2010 20:05:41 -0300
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" <firstname.lastname@example.org>
To: "Lua list" <email@example.com>
Sent: Sunday, March 07, 2010 6:12 PM
Subject: Re: Saving a table in file
This one is http://www.lua.org/pil/12.1.html 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 <firstname.lastname@example.org> wrote:
This version handles nested tables: http://lua-users.org/wiki/PickleTable
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 = io.open(fileName, "w")
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) ==
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)