快速掌握Lua 5 3 兩個完整的例子

2021-07-09 13:41:26 字數 4688 閱讀 8124

將lua指令碼作為乙個小型的簡化版的「資料庫」,展現了如何簡單高效的將這個「資料庫」中的資料轉化為網頁顯示。

-- "db.lua"檔案中內容。

--[[ 這裡看似是一張名為"entry"的表中儲存了許多資料,

實際上在轉化程式中這時乙個名為"entry"的函式,

引數是下面這個儲存了許多資料的"table"。

還記得"foo({})"與"foo{}"等同嗎?]]

entry

-- "main.lua"檔案中內容。

function

fwrite

(fmt, ...)

-- 可變引數由"..."傳遞。

for i = 1, select('#', ...) do

arg[i] = select(i, ...) -- 這裡select()返回了i之後所有的值,但多餘的值被拋棄了。

endreturn

io.write(string.format(fmt, table.unpack(arg)))

endfunction

begin

() io.write([[

here are brief descriptions of some projects around the

world that use lua.

]])end

function

entry0

(o) n=n + 1

local title = o.title or

'(no title)'

fwrite('%s\n', n, title)

endfunction

entry1

(o) n=n + 1

local title = o.title or o.org or

'org'

fwrite('\n\n')

if o.description then

fwrite('%s', string.gsub(o.description,

'\n\n\n*', '\n'))

fwrite('\n')

endif o.email then

fwrite('contact: %s\n',

o.email, o.contact or o.email)

elseif o.contact then

fwrite('contact: %s\n', o.contact)

endendfunction

end()

fwrite('

\n')

endbegin()

n = 0

entry = entry0 -- 為"entry"賦值不同的邏輯,用不同的邏輯操作"db.lua"中的"table"。

fwrite('\n')

n = 0

entry = entry1 -- 為"entry"賦值不同的邏輯,用不同的邏輯操作"db.lua"中的"table"。

dofile('db.lua') -- 載入"db.lua"中的內容並執行。相當於呼叫"entry()",以"table"作為引數。

end()

$ lua x.lua > test.html

是實現」markov chain algorithm」。」markov chain algorithm」就是滿足一些值與另一些值之間的對映關係的值的序列。舉個例子會更容易理解,

the more we try the more we do

這個就是個值(每個單詞算作乙個值)的序列,這個值的序列從左至右滿足如下的對映關係,

[\n \n] =     -- 初始的預設值是"\n",所以"\n \n"之後跟著"the"。

[\n the] = -- "\n the"之後跟著"more"。

[the more] = -- "the more"之後跟著"we"。因為出現了兩個"the more",所以這裡有兩個"we"。

[more we] = -- "more we"之後跟著"try"或者"do"。

[we try] = -- "we try"之後跟著"the"。

[try the] = -- "try the"之後跟著"more"。

[we do] = -- "we do"之後跟著"\n"。

接下來的例子就是輸出給定的值的序列的」markov chain」,索引值限定為兩個,

-- 返回乙個"iterator"。每次被呼叫,從給定的值的序列中找出乙個單詞。

function

allwords

() local line = io.read() -- 輸入給定的值的序列。

local pos = 1

-- 值的序列中搜尋到了**。

return

function

() -- "iterator function"。

while line do

-- 從給定的值的序列中找出乙個單詞,返回單詞的起始和終止位置。

local s, e = string.find(line, "%w+", pos)

if s then

-- 找到了乙個單詞。

pos = e + 1

-- 更新搜尋位置。

return

string.sub(line, s, e) -- 返回找到的單詞。

else

-- 沒找到單詞。

line = io.read() -- 讀取下一行。

pos = 1

-- 值的序列中搜尋位置重置。

endend

return

nilend

end-- 將兩個索引值組合成為乙個索引。

function

prefix

(w1, w2)

return w1 .. ' ' .. w2

end-- 將索引與值的對映存入"statetab"。

function

insert

(index, value)

ifnot statetab[index] then

-- 乙個索引可能對應多個值(["more we"] = ),所以"table"儲存。

statetab[index] = {}

endtable.insert(statetab[index], value)

endlocal n = 2

-- 索引值限定為兩個。

local maxgen = 10000

-- 最大產生的結果數量。

local noword = "\\n"

-- 預設的索引值是"\n"。

-- 建立對映關係並儲存。

statetab = {} -- 儲存對映關係的"table"。

local w1, w2 = noword, noword -- 初始的兩個索引值都是"\n"。

for w in allwords() do

-- 產生索引值。

insert(prefix(w1, w2), w) -- 儲存對映關係。

w1 = w2; w2 = w; -- 向後依次更替索引值。

endinsert(prefix(w1, w2), noword) -- 儲存最後乙個對映關係([we do] = )。

-- 列印"statetab"中的內容。

for k, v in

pairs(statetab) do

io.write(string.format("[%s] = \n")

end--[[ 以初始索引在"statetab"中隨機取值,

不斷的以新的值與舊的索引值組合成為新的索引,再次在"statetab"中隨機取值,

迴圈往復,列印出找打的值。]]

w1 = noword; w2 = noword -- 重新初始化索引值為預設的索引值。

for i = 1, maxgen do

-- 最大結果數量為"maxgen"個。

local list = statetab[prefix(w1, w2)]

-- 產生隨機數種子。

math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 6)))

--[[ 從"list"中隨機選擇乙個元素,

比如[more we] = 對應"try"和"do"兩個元素,隨機選擇乙個。]]

local r = math.random(#list) -- 生成隨機數。

local nextword = list[r]

if nextword == noword then

-- 如果到了預設的索引值,就不再找了。

break

endio.write(nextword, " ")

w1 = w2; w2 = nextword -- 不斷的以新的值與舊的索引值組合成為新的索引。

endio.write("\n")

$ lua markov_chain.lua

the more we try the more we do

快速掌握Lua 5 3 從Lua中呼叫C函式

a 1 程式主體在c中執行,c函式註冊到lua中。c呼叫lua,lua呼叫c註冊的函式,c得到函式的執行結果。2 程式主體在lua中執行,c函式作為庫函式供lua使用。第一種方式看起來很羅嗦,也很奇怪。既然程式主體執行在c中,而且最終使用的也是c中定義的函式,那麼為何要將函式註冊給lua,然後再通過...

高速掌握Lua 5 3 擴充套件你的程式 1

a config.lua 檔案裡 window size width 200 height 300 main.c 檔案裡 include include include include include include void error lua state l,const char fmt,voi...

教你快速掌握兩個分頁儲存過程的用法

兩個分頁儲存過程的用法 basic pagination2005 只能在sqlserver2005下用 basic pagination2000 可在sqlserver2000和sqlserver2005下通用 兩個儲存過程的引數是一樣的,其中的引數說明在 中已有注釋。需要注意的是當 isrecou...