*Luaリファレンス 要注意点 ~データ永続~ [#kca74370]

**自己記述データフォーマット [#t0a2b961]
-以下のようなデータファイルを"data.txt"として用意しておく。
#sh(lua){{
Entry
{
    auther = "Donald E. Knuth",
    title = "Literate Programming",
    publisher = "CSLI",
    year = 1992,
}

Entry
{
    auther = "Jon Bentley",
    title = "More Programming Pearls",
    year = 1990,
    publisher = "Addison-Wesley",
}

}}
~
Entry{・・・} は Entry({・・・})のシンタックスシュガーであるから、~
そのようなソースが展開されるという前提で記述すればよい。

以下のように読み込める


#sh(lua){{
local authors = {}
function Entry (b) authors[b.auther] = true end
dofile("data.txt")
for name in pairs(authors) do print(name) end
}}
~
**シリアライズ書き出し関数 [#kac36669]
-以下のようなデータファイルを"data.txt"として用意しておく。
#sh(lua){{
function serialize (o)
    if type(o) == "number" then
        io.write(o)
    elseif type(o) == "string" then
        io.write(string.format("%q", o))   -- string.format("%q", str)でquoteとなる。適切にシリアライズして書き出しておかないと、読み込みした時にえらいこととなる。
    else
        <その他の場合>
    end
end
}}
-シリアライズの決め手はない
LUAには残念ながらシリアライズの決め手となるものがない。~
このあたりは、複雑なデータ構造でもまんまシリアライズ出来るPythonには、大きく劣ると言える。~
とはいえ、そもそもLUAの使用用途として、LUA自体でシリアライズするということが不自然なのかもしれない。~


}}



トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS