lua 入門(一)

寫這些都是針對有經驗又不想看文檔的程序員html

Lua 中有八種基本類型: nil、boolean、number、string、function、userdata、 thread 和 table。git

nil至關於其它語言的null,

 false和nil爲假,其它均爲真

 表、函數、線程、以及徹底用戶數據在 Lua 中被稱爲 對象: 變量並不真的 持有 它們的值,而僅保存了對這些對象的 引用。

 table 是hash隨機存儲key,因此for循環輸出一個table時並不會按定義時的順序輸出

變量定義程序員

name = value          --全局變量

 local name = value  --局部變量   

 local tbl      = {}       --空的table

 tbl.name     =  value

 tal['name']   =  value

語句控制結構github

while exp do block end

repeat block until exp

if exp then block {elseif exp then block} [else block] end

goto 語句函數

goto Name     --goto到指定標籤名 Name 首字母不能是小寫 能夠爲 "_"

:: Name ::        --標籤名

for 語句lua

for val=e1,e2,e3 do block end 
 例:
      for val=1,9 do print(val) end --輸出1到9 
 

 for namelist in explist do block end

例:

 local list = {t=1,e=2,s=3,t=4}

 for k,v in pairs(list) do print(k,v) end

函數定義線程

該語句3d

function f () body end

被轉譯成code

f = function () body end

該語句htm

function t.a.b.c.f () body end

被轉譯成

t.a.b.c.f = function () body end

該語句

local function f () body end

被轉譯成

local f; f = function () body end


PS: 函數使用須要先聲明

其它:

取字符串長度 #str

 字符鏈接:「..」

 table 下標是從1開始

string.gsub 簡單例子

local arr = {bidword=12321,plandid=3456}

local x = "http://catct.cn/?bidword={bidword}&{plandid}"

local str = string.gsub(x,"{(%w+)}",arr)

--輸出結果:http://catct.cn/?bidword=12321&3456

函數調用(self 的使用)

local x = {}

function x:test( str )

    print(str)              --call function 

    self.echo ('test')    --test

    self:echo ('test')    --table: 0xabee40     test

    self.print ('test')     --輸出空

    self:print ('test')     --test

end


function x.echo( ... )

    print(...)

end


function x:print( ... )

    print(...)

end


x:test('call function')

參考文檔: http://cloudwu.github.io/lua53doc/manual.html

相關文章
相關標籤/搜索