加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua 入门(一)

发布时间:2020-12-14 21:57:17 所属栏目:大数据 来源:网络整理
导读:写这些都是针对有经验又不想看文档的程序员 Lua 中有八种基本类型: nil、boolean、number、string、function、userdata、 thread 和 table。 nil相当于其它语言的null, false和nil为假,其它均为真 表、函数、线程、以及完全用户数据在 Lua 中被称为 对象

写这些都是针对有经验又不想看文档的程序员

Lua 中有八种基本类型: nil、boolean、number、string、function、userdata、 thread 和 table。

nil相当于其它语言的null,

 false和nil为假,其它均为真

 表、函数、线程、以及完全用户数据在 Lua 中被称为 对象: 变量并不真的 持有 它们的值,而仅保存了对这些对象的 引用。

 table 是hash随机存储key,所以for循环输出一个table时并不会按定义时的顺序输出

变量定义

name = value          --全局变量

 local name = value  --局部变量   

 local tbl      = {}       --空的table

 tbl.name     =  value

 tal['name']   =  value

语句控制结构

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 语句

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

函数定义

该语句

function f () body end

被转译成

f = function () body end

该语句

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读