现在的位置: 首页 > 综合 > 正文

Lua语言基础

2018年09月13日 ⁄ 综合 ⁄ 共 993字 ⁄ 字号 评论关闭
文章目录

lua

脚本语言

脚本文件都是在载入时解释和编译(不是预编译,而是在调用时才处理)。以lua为列,它只有在载入时才被编译成二进制形式并存在于内存中,直到被释放。

注释

--this is comment

--[[

this is comment

this is comment

this is comment

--]]

输出

print("hello world!")

 

变量

变量类型:

nil --变量被赋值为nil,表示删除该变量

Boolean

string

Number

table  --变量时table对象的引用

 

type()  --查看变量类型

 

类型转换

tonumber(myString)

tostring(myNumber)

 

string

string.char(n1,n2,...)  --返回ASCII对应字符

string.len(myString)  --字符长度

string.sub(myString, start, end)

string.find()

string.format()

 

table

table.getn(myTable) --返回table中元素的个数

table.sort()

table.insert(myTable, position, value)

table.remove(myTable, position)

pairs() --在遍历非数字索引的table时非常有用

for index, value in pairs(myTable) do

print(index, value)

end 

 

函数

参数列表

--[[

    对于参数...

lua会创建一个局部的名字为argtable,保存所用调用时传递的参数

Arg.n能获取参数个数

--]]

 

function Test(...)

if arg.n > 0 then

    for indx = 1, arg.n do

        print(arg[indx])

    end

end

end 

 

返回值

可返回多个结果,用逗号隔开

 

数学运算函数

Lua提供函数级别的可以调用C标准库的数学运算函数。

math.max()

math.min()

math.abs()

math.cos()

math.floor()  --向下取整,如果想四舍五入,那么先给它加上0.5,再向下取整

math.pi()

math.random()  --生成0~1之间的伪随机数

【上篇】
【下篇】

抱歉!评论已关闭.