|
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" <btnfdp.lobato@gmail.com>
To: "Lua list" <lua@bazar2.conectiva.com.br> 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 <aleirade@sct.microlink.com.br> wrote:This version handles nested tables: http://lua-users.org/wiki/PickleTable Cheers, Andre 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") 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) else k = tostring(k) end if type(v) == 'string' then v = string.format("%q", v) else v = tostring(v) end output:write(string.format("[%s] = %s,\n", k, v) end end output:close() end NFF