lua基本语法

3.1k 词
关键字
1
2
3
4
and	break	do	else	elseif
end false for function if
in local nil not or
repeat return then true until while
运算
  • 数字运算

    1
    2
    支持+,-,*,/,…… 比如2^2 结果为4,2^4结果为16
    连接两个字符串,用".."运算符
  • 赋值运算

    1
    2
    3
    4
    a,b,c,d=1,2,3,4  --多变量一起赋值
    a,b=b,a --交换变量功能
    在默认情况下,变量是全局的。如果需要使用局部变量,使用关键字loacal
    local a,b,c =1,2,3 --a,b,c都是局部变量
  • 逻辑运算

    1
    2
    3
    4
    5
    6
    7
    8
    9
    and,or,not
    在lua 中,只有false和nil被计算为false,其他数据都被计算为true,0也是true
    and和or 的运算结果不是true和false,而是和它的两个操作数相关

    a and b :如果a为false,则返回a,否则返回b
    a or b :如果a为true,则返回a,否则返回b

    模拟c语言中的语句:x=a?b:c,在lua中,可以写成:x=a and b or c都是局部变量
    有用的语句:x=x or v,相当于: if not x then x=v end
条件判断语句
  • if

    1
    2
    3
    4
    5
    if condition then
    ...
    elseif condition then
    ... else ...
    end
  • repeat

    1
    repeat ... until condition
  • while

    1
    2
    3
    4
    while condition
    do
    ...
    end
  • for

    1
    2
    3
    4
    5
    6
    7
    8
    9
    for var=v1,vn,step
    do
    ...
    end

    for var1,var2,... varn in table
    do
    ...
    end
  • 闭包

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    eg one.
    function newCounter()
    local i=0
    return function() -- anonymous function
    i=i+1
    return i
    end
    end

    c1=newCounter()
    print(c1()) -->1
    print(c1()) -->2

    eg two.
    function myPower(x)
    return function(y) return y^x end
    end

    power2=myPower(2)
    power3=myPower(3)

    print(power2(4))
    print(power3(5))
常用结构
  • table
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    table 是lua的重要数据类型
    table 类似于 python中的字典
    table 只能通过构造式来创建

    lua code

    tab={a=10,b=20,c=30,d=40}
    print(tab["a"])

    comment:
    a)table中的每项要求是 key=value的形式
    b)key只能是字符串,这里的a,b,c,d都是字符串,但是不能加引号
    c)通过key来访问table的值,这时候 a必须加引号


    tab={10,m=20,11,12,13}
    print(tab[1]) =10
    print(tab[2]) =11
    print(tab[3]) =12
    print(tab[4]) =13

    get table length
    print(table.getn(tab)) -> 4
    print(#tab) -> 4

    for k,v in pairs(tab) do
    print(k,":",v)
    end

    1:10
    2:11
    3:12
    4:13
    m:20

    注释:
    a)省略key,会自动以1开始编号,并跳过设置过的key
    b)获取表长度,只有当表使用1自动编号开始,可以获取
lua表空判断
1
2
3
4
5
local a ={}
判断table a是否为空,有提供api table.maxn(a) 或 #a 获取表a长度,
该情况只针对key为数字且安装递增方式。

对于自定义key的情况下,获取表长度会失败,可通过lua内置next函数解决
MetaTable和MetaMethod

MetaTable和MetaMethod是lua中的重要语法,MetaTable用来做类似C++重载操作符的功能

假如有两个函数:

1
2
fraction_a ={numerator=2,denominator=3}
fraction_b ={numerator=4,denominator=7}

如果要实现分数见相加:2/3+4/7,执行fraction_a+fraction_b会报错
如果使用MetaTable,则如下

1
2
3
4
5
6
7
8
9
10
11
12
fraction_op={}
function fraction_op.__add(f1,f2)
ret = {}
ret.numerator=f1.numerator*f2.denominator+f2.numerator*f1.denominator
ret.denominator=f1.denominator*f2.denominator
return ret
end

setmetatable(fraction_a,fraction_op)
setmetatable(fraction_a,fraction_op)

fraction_s = fraction_a + fraction_b

至于__add是MetaMethod,这是lua内建约定的,还有如下的MetaMethod:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
__add(a,b)  	对应a+b
__sub(a,b) 对应a-b
__mul(a,b) 对应a*b
__div(a,b) 对应a/b
__mod(a,b) 对应a%b
__pow(a,b) 对应a^b
__unm(a) 对应-a
__concat(a,b) 对应a .. b
__len(a) 对应#a
__eq(a,b) 对应a==b
__lt(a,b) 对应a<b
__le(a,b) 对应a<=b
__index(a,b) 对应a.b
__newindex(a,b,c) 对应a.b=c
__call(a,...) 对应a(...)

Lua 5.1 参考手册
lua基本语法
Programming in Lua


您的鼓励是我写作最大的动力

俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。

  						<div id="donate_guide" class="donate_bar center ">
      					<a href="http://mypay-1253516637.costj.myqcloud.com/2000.jpg" title="支付宝打赏" class="fancybox" rel="article0" style="float:left;margin-left:25%;margin-right:2px;">
      					<img src="http://mypay-1253516637.costj.myqcloud.com/2000.jpg" title="支付宝打赏" height="164px" width="164px"/>
      					</a> 
  						</div>