Lua編程示例(三):稀疏表、雙端隊(duì)列、格式化輸出、表和循環(huán)表的格式化輸出
更新時(shí)間:2015年07月09日 10:36:27 投稿:junjie
這篇文章主要介紹了Lua編程示例(三):稀疏表、雙端隊(duì)列、格式化輸出、表和循環(huán)表的格式化輸出,本文直接給出實(shí)例代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下
a={} for i=1,10 do a[i]={} for j=0,10 do if(i%2==0) then a[i][j]=0 end end end print(a[9][10]) print(a[10][10]) print() --雙端隊(duì)列 List={} function List.new() return {first = 0,last = -1} end function List.pushleft(list,value) local first= list.first-1 list[first] = value list.first= first end function List.pushright(list,value) local last = list.last+1 list[last]= value list.last=last end function List.popleft(list) local first=list.first if(first>list.last) then error("list is empty") end local res= list[first] list[first]=nil list.first=list.first+1 return res end function List.popright(list) local last = list.last if last<first then error("the list is empty") end local res = list[last] list[last]= nil list.last= list.last-1 return res end function List.display(list) if(list.first>list.last) then error("the list is empty",2) end for i=list.first ,list.last do print(list[i]) end end mylist=List.new() List.pushleft(mylist,12) List.pushleft(mylist,"00") List.pushright(mylist,34) List.pushright(mylist,56) List.display(mylist) print() function newStack () return {""} end function serialize(o) if type(o) == "number" then io.write(o) elseif type(o) == "string" then --不要手動(dòng)加入引號(hào),否則會(huì)有邊際效應(yīng) io.write(string.format("%q",o)) elseif type(o) == "table" then io.write("{\n") for i,v in pairs(o) do io.write(" "..i.." = ") serialize(v) io.write(",\n") end io.write("}\n") end end serialize(123) print() serialize("112233") print() tab = { a=11,haha="www" ,c=333} serialize(tab) function basicSerialize (o) if type(o) == "number" then return tostring(o) else return string.format("%q", o) end end function save (name, value, saved) saved = saved or {} -- 參數(shù)未傳入的初始化 io.write(name, " = ") if type(value) == "number" or type(value) == "string" then io.write(basicSerialize(value), "\n") elseif type(value) == "table" then if saved[value] then io.write(saved[value], "\tcircle\n") else saved[value] = name io.write("{}\n") for k,v in pairs(value) do local fieldname = string.format("%s[%s]", name, basicSerialize(k)) save(fieldname, v, saved) end end else error("cannot save a " .. type(value)) end end a = {x=1, y=2; {3,4,5}} a[2] = a -- 循環(huán)表 a.z = a[1] -- 共享子表 save('a',a)
運(yùn)行結(jié)果:
nil 0 00 12 34 56 123 "112233" { a = 11, c = 333, haha = "www", } a = {} a[1] = {} a[1][1] = 3 a[1][2] = 4 a[1][3] = 5 a[2] = a circle a["y"] = 2 a["x"] = 1 a["z"] = a[1] circle
相關(guān)文章
Lua中的loadfile、dofile、loadstring、require用法實(shí)例
這篇文章主要介紹了Lua中的loadfile、dofile、loadstring、require用法實(shí)例,需要的朋友可以參考下2015-04-04Lua編程示例(六): C語言調(diào)用Lua函數(shù)
這篇文章主要介紹了Lua編程示例(六): C語言調(diào)用Lua函數(shù),本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-07-07Lua中的迭代器和泛型for學(xué)習(xí)總結(jié)
這篇文章主要介紹了Lua中的迭代器和泛型for學(xué)習(xí)總結(jié),本文講解了迭代器和泛型for的基礎(chǔ)知識(shí)、泛型for的語義、無狀態(tài)的迭代器等內(nèi)容,需要的朋友可以參考下2014-09-09Lua判斷字符串中包含中文字符的方法和計(jì)算字符串寬度函數(shù)分享
這篇文章主要介紹了Lua判斷字符串中包含中文字符的方法和計(jì)算字符串寬度函數(shù)分享,需要的朋友可以參考下2015-04-04