持久化
写一个文件容易,读一个文件难。健壮的程序应该能正确读取文件,还能处理坏文件。
数据描述是Lua的主要应用之一,从Lua发明以来,我们花了很多心血使他能够更快的编译和运行大的chunks。也即是说lua文件即可存储数据。
--使用lua文件作为数据数据,并进行管理的例子。-------------- 数据文件data.luaEntry{author = "Donald E. Knuth",title = "Literate Programming",publisher = "CSLI",year = 1992}Entry{author = "Jon Bentley",title = "More Programming Pearls",publisher = "Addison-Wesley",year = 1990}-------------- 数据文件data.lualocal authors = {} -- a set to collect authorsfunction Entry (b) -- Entry即可作为统计工具,记录数据。if b.author thenauthors[b.author] = trueendend
序列化
不带循环嵌套的表
function serialize (o)if type(o) == "number" thenio.write(o)elseif type(o) == "string" thenio.write(string.format("%q", o))elseif type(o) == "table" then-- 如果是以下这种循环或者嵌套的表,就会死循环。-- a = {x=1, y=2; {3,4,5}}-- a[2] = a -- cycle-- a.z = a[1]a = {}io.write("{\n")for k,v in pairs(o) doio.write(" [")serialize(k)io.write("] = ")serialize(v)io.write(",\n")endio.write("}\n")elseerror("cannot serialize a " .. type(o))endendlocal t = {a=12,b='Lua',key='another "one"'}serialize(t){["a"] = 12,["b"] = "Lua",["key"] = "another \"one\"",}
带循环嵌套的表
function basicSerialize (o) --一般序列化:number、stringif type(o) == "number" thenreturn tostring(o)else -- assume it is a stringreturn string.format("%q", o)endend-- name :key值-- value :value值,可能是自循环table-- saved :save表,保存已经序列化过的table,反之自循环table导致的死循环。键值对:value(table) - namefunction save(name, value, saved)saved = saved or {}io.write(name, " = ")if type(value) == "number" or type(value) == "string" thenio.write(basicSerialize(value), "\n")elseif type(value) == "table" thenif saved[value] thenio.write(saved[value], "\n")elsesaved[value] = name -- save name for next timeio.write("{}\n") -- create a new tablefor k,v in pairs(value) do -- save its fieldslocal fieldname = string.format("%s[%s]", name,basicSerialize(k))save(fieldname, v, saved)endendelseerror("cannot save a " .. type(value))endend----------------------------------------a = {x=1, y=2; {3,4,5}}a[2] = aa.z = a[1]save('a', a) --序列化自循环表a = {}a[1] = {}a[1][1] = 3a[1][2] = 4a[1][3] = 5a[2] = aa["y"] = 2a["x"] = 1a["z"] = a[1]----------------------------------a = {{"one", "two"}, 3}b = {k = a[1]}save('a', a) -- 不同save表save('b', b) -- 不同save表a = {}a[1] = {}a[1][1] = "one"a[1][2] = "two"a[2] = 3b = {}b["k"] = {}b["k"][1] = "one"b["k"][2] = "two"--------------------------------a = {{"one", "two"}, 3}b = {k = a[1]}local t = {}save('a', a, t) --共同save表save('b', b, t) --共同save表a = {}a[1] = {}a[1][1] = "one"a[1][2] = "two"a[2] = 3b = {}b["k"] = a[1]
