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

相关文章
相关标签/搜索