Lua Persistence

from http://www.lua.org/slides.html

function save(i,v) 

local t=type(v) write(i..’=‘) if t==’nil’ then write(‘nil’) elseif t==‘number’ then write(v) elseif t==‘string’ then write(‘“‘.v..’”’) elseif t==‘table’ then write_record(v) end end

function write_record(t) local i,v=next(t,nil) write(‘{’) while i do save(i,v) i,v=next(t,i) if i then write(‘,’) end end write(“) end

writeto(“state”) — save env to file i,v=nextvar(nil) while i do save(i,v) i,v=nextvar(i) end

dofile(“state”) — restore env from file }

orpeth.com