Lua脚本之语法基础快速入门

Lua的语法基础超级简单,非常易于上手,下面总结一些学习过程中的Lua语法基础。


在开始学习之前,先介绍一些最基本的概念,在Lua中具有一个代码块的概念,每个函数或者for循环等都是一个代码块。在Lua中,用 “- - ”来标记该行的注释,使用“- - [ [” 和   “ - - ] ] ”之间括起来的部分进行块注释。如下所示:

1
2
3
4
5
6
7
-- 行注释,仅仅注释当前行  
for  idx = 1, 10  do   --在代码之后进行行注释  
      print( "idx=" ,idx);    
end  
--[[  
块注释,上边的 for 循环结构跟end结合起来就是一个Lua中常见的代码块结构。  
--]]

另外,Lua中支持的算术运算符有:+、-、*、/,即加、减、乘、除;支持的关系运算符有:==、~=(不等于)、<、>、<=、>=;支持的逻辑运算符有:and、or、not。需要注意的是,在Lua中,and逻辑运算符如果第一个参数是false,则返回false,不会执行第二个参数的代码(即使第二个参数是一个错误的表达式,也能顺利运行);如果第一个参数是true,返回第二个参数的值。 同理,or逻辑运算符如果第一个参数不是false,则返回第一个参数的值(不会执行第二个参数的代码);否则返回第二个参数的值。这就是所谓的逻辑运算符短路求值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
result =  true  
if  result and an_donot_defined_method() then  
   print( "no erro occured!" )  
end  
--[[  
上述代码输出的错误如下:  
stdin:1: attempt to call global  'an_donot_defined_method'  (a nil value)  
stack traceback:  
     stdin:1: in main chunk  
     [C]: ?  
--]]  
result = false   
if  (result and an_donot_defined_method())== false   then  
   print( "no erro occured!" )  
end  
--上述代码顺利通过编译,即使有一个没定义的方法,打印结果:no erro occured!


一、基本数据类型

Lua中具有5种基本的数据类型:nil、Boolean、string、Number和table。在Lua中使用变量不需要提前声明,变量的类型决定于用户赋值的类型。可以使用 type()函数判断变量的类型。其中,nil、Boolean、Number都是用法比较简单的类型,string、table类型用法稍微复杂点。给一个变量赋值为nil,表示释放该变量。Boolean跟其他语言一样,只有true和false两种值。Number是双精度浮点数,Lua中没有整数类型。table类型可以当作数组使用。

在Lua中,变量默认是全局的,这通常导致一些调试困难,最好尽量显式的在变量名之前加上 local 关键字声明该变量为局部变量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
gNumber = 10  --这是一个默认全局的变量  
print(type(gNumber))  
--输出结果为number  
gNumber = nil --之前的number类型gNumber = 10变量被释放  
print(type(gNumber))  
--输出结果为nil  
   
function LocalVarFunction ()  
   local pTable = {} --用local关键字声明一个局部变量,这个变量将在执行LocalVarFunction方法后销毁  
      for  idx = 1, 5  do   
            local result =  true   --这个result将在每个 for 循环执行之后销毁  
            if  result then   
               local pString =  "这个字符串将在if代码块之后销毁"  
               pTable[idx] = pString  
               print(pTable[idx])  
            end  
      end  
end


下面详细介绍string以及table两种类型的详细用法。

1、string类型的用法

Lua中的字符串操作非常出色。下表是一些特殊意义的字符:

特殊的Lua字符串

QQ截图20141121173412.png

a、类型转换

Lua会根据上下文在合理合法的情况下隐式进行数字和字符之间的转换。另外,也可以使用tonumber()函数和tostring()函数显式地进行字符与数字的转换。 见代码实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--字符与数字的隐式转换  
print( "10"  + 7)  
--输出结果为:17,将字符10隐私转化为Number类型计算  
print( "hello"  + 7)  
--无法进行运算,即不能隐式将 "hello" 字符转化为Number计算  
--[[  
    系统错误如下:  
stdin:1: attempt to perform arithmetic on a string value    
stack traceback:    
     stdin:1: in main chunk    
     [C]: ?    
--]]  
   
   
   
--字符与数字的显式转换  
print(tonumber( "100" )+11)  
--输出结果为:111  
print(type(tostring(100)))  
--输出结果为:string

b、常用的字符处理函数介绍    

  • string.char()函数根据传入的ASCII编码返回该编码对应的字符。如:string.char(10),表示字符换行符,10是换行符的ASCII编码。

  • string.len()函数求字符串的长度。如:

1
2
print(string.len( "hello" ))  
--输出结果为:5
  • string.sub(aString, start, end) 函数返回指定字符串的子串。如:

1
2
3
gString =  "hello Lua"  
print(string.sub(gString, 7,9))  
--输出结果为:Lua
  • string.format()函数格式化输出指定字符串。%s表示字符串,%d表示所有数字,%D表示非数字,%a表示字母,%c表示控制字符,%l小写字母,%p标点符号,%s空格符号,%u大写字母,%w字母数字,%x十六进制数,%z用0表示的字符。加%前缀可以让特殊符号也能用在格式化中(如:().%+_*?[ ^ $ ]),如%%代表百分比符号。%.4f表示小数点后有4位的浮点数,d.表示至少有两个数字的整数,如果不足两个数字则用0补足。如:

1
2
3
4
aString =  "哈哈,你是"  
bString =  "一头猪"  
print(string.format( "%s%s" , aString, bString))  
--输出结果为:哈哈,你是一头猪
  • sting.find(sourceString, targetString) 函数在sourceString字符串中查找第一个符合targetString字符串的位置,如果找到则返回开始和结束的位置,没找到则返回nil。

  • string.gsub(sourceString, pattern, replacementString) 函数返回一个字符串,sourceString字符中满足pattern格式的字符都会被替换成replacementString参数的值。

  • string.gfind(sourceString, pattern) 函数遍历一个字符串,一旦查找到符合指定格式的字符串就返回该子串。


2、table类型的用法

一般table可以当做数组使用,可以通过table[n]的索引形式访问任意数组中的某个成员。在Lua中,table还能被当做字典dictionary数据使用,并且数组跟字典的用法还能混合使用(实质上还是数组,只不过索引从数字变成其他属性值)。

a、使用其他值作为table的索引以及多维table

table还可以使用其他的值作为索引值,并且能用数字跟其他值同时作为同一个table的索引。如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
gTable = {}  
gTable.name =  "eric"  
gTable.gender =  "man"  
gTable.phonenumber =  "0000000000"  
gTable[1] =  "公司"  
gTable[2] =  "部门"  
for  index, value in pairs(gTable)  do   
   print(index, value)  
end  
--[[  
输出结果如下:  
1   公司  
2   部门  
phonenumber 0000000000  
gender  man  
name    eric  
--]]


注意,上述循环中的pairs()函数可以遍历table中的每一对值(索引以及索引对应的value,有点类似字典,不是吗?)

事实上,table的索引还可以是table本身,这样就组成了一个多维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
gTable = {}  
gTable.name =  "eric"  
gTable.gender =  "man"  
gTable.phonenumber =  "0000000000"  
gTable[1] =  "公司"  
gTable[2] =  "部门"  
gTable.hobby = { "跑步" "读书" "游戏" "动漫" }  -- 多维table,可以通过gTable.hobby[1]的方式访问.即gTable.hobby本身也是一个table  
gTable.secTable = {}  
gTable.secTable.job =  "程序员"  
gTable.secTable.label =  "写代码的"  
gTable.secTable.description =  "职责是实现产品的逻辑"  
   
for  index, value in pairs(gTable)  do   
   print(index, value)  
   if  ( "table"  == type(value)) then  
      for  idx, var in pairs(value)  do   
          print( "二维table:" , idx, var)  
      end  
    end  
end  
--[[  
输出结果如下:  
1   公司  
2   部门  
hobby   table: 0x7fdceac14bc0  
二维table:    1   跑步  
二维table:    2   读书  
二维table:    3   游戏  
二维table:    4   动漫  
phonenumber 0000000000  
gender  man  
secTable    table: 0x7fdceac15100  
二维table:    label   写代码的  
二维table:    description 职责是实现产品的逻辑  
二维table:    job 程序员  
name    eric  
--]]


b、table 的常用函数

  • table.getn()函数,返回table中元素的个数。如:

1
2
3
4
gStringTable = { "a" "b" , "c" , "d" , "e" }  
for  i = 1, table.getn(gStringTable)  do  
      print(gStringTable[i])  
end
  • table.sort()函数,将table中的元素从小到大排列。如:

1
2
3
4
5
6
7
8
9
10
11
12
gNumberTable = {10, 5, 7, 2,3, 2}  
table.sort(gNumberTable)  
for  i = 1, table.getn(gNumberTable)  do   
    print(gNumberTable[i])  
end  
--输出结果如下:  
2  
2  
3  
5  
7  
10
  • table.insert(pTable, position, value) 函数在table中插入一个新值,位置参数如果没指定,则默认将新值插入到table的末尾。

  • table.remove(pTable, position) 函数从指定table中删除指定位置的元素并返回该元素,如果没有指定删除的位置,则默认删除table的最后一个元素。

介绍到这里, Lua中基本的数据类型诸位应该都能掌握,休息一下,下面接着开始简单介绍Lua的基本语句以及函数。


二、Lua中的常用语句结构以及函数

1、Lua中的常用语句结构介绍

1
2
3
4
5
6
7
8
9
-- if  语句结构,如下实例:  
gTable = { "hello" , 10}  
if  nil ~= gTable[1] and  "hello"  == gTable[1] then  
   print( "gTable[1] is" , gTable[1])  
elseif  10 == gTable[2] then  
   print( "gTable[2] is" , gTable[2])  
else   
   print( "unkown gTable element" )  
end
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
-- while  和repeat循环语句结构, while 先判断条件,如果 true 才执行代码块(有可能跳过该代码块);repeat则是在最后判断条件,保证代码块至少执行一次。  
gTable = {1,2,3,4,5,6,7,8,9,10}  
index = 1  
while  gTable[index] < 10  do   
    print( "while gTable[" ,index, "] is " ,gTable[index])  
    index = index + 1 -- 注意,Lua不支持index++或者index += 1形式的运算符。  
end  
--[[  
while 循环输出结果如下:  
while  gTable[   1   ] is    1  
while  gTable[   2   ] is    2  
while  gTable[   3   ] is    3  
while  gTable[   4   ] is    4  
while  gTable[   5   ] is    5  
while  gTable[   6   ] is    6  
while  gTable[   7   ] is    7  
while  gTable[   8   ] is    8  
while  gTable[   9   ] is    9  
--]]  
   
--上一个循环结束后,index = 10  
repeat  
     print( "repeat gTable[" ,index, "] is " ,gTable[index])  
    index = index - 2  
until index < 1  
--[[  
输出结果如下:  
repeat gTable[  10  ] is    10  
repeat gTable[  8   ] is    8  
repeat gTable[  6   ] is    6  
repeat gTable[  4   ] is    4  
repeat gTable[  2   ] is    2  
--]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- for 循环结构, for 循环结构具有三个参数,初始值,结束值,每个循环增加值。  
for  index = 1, 5  do  --不设置第三个参数的话,默认缺省第三个参数是1,即每个循环 index 增加1  
    print( "for cycle index =" ,index)  
end  
--[[  
输出结果为:  
for  cycle index =   1  
for  cycle index =   2  
for  cycle index =   3  
for  cycle index =   4  
for  cycle index =   5  
--]]  
   
for  index = 20 , 0, -5  do  --设定第三个参数为-5  
  print( "for cycle index:" ,index)  
end  
--[[  
输出结果:  
for  cycle index:    20  
for  cycle index:    15  
for  cycle index:    10  
for  cycle index:    5  
for  cycle index:    0  
--]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- break 关键字可以使循环强制退出,Lua中没有 continue 关键字,需要通过其他方式实现 continue 关键字,比如 if - else 语句。或者通过网络下载Lua的 continue 关键字补丁安装来解决该问题  
   
for  index = 1, 100, 5  do   
   if  index > 10 and index < 25 then  --用 if - else 语句实现 continue 关键字的功能  
      print( "continue!!!!! index=" ,index)  
   else   
     if  index > 15 and index < 35 then   
        print( "break~~~~~index=" ,index)  
        break  
     end  
     print( "At end index=" ,index)  
   end  
end  
   
--[[  
输出结果如下:  
At end index=   1  
At end index=   6  
continue !!!!! index=    11  
continue !!!!! index=    16  
continue !!!!! index=    21  
break ~~~~~index=    26  
--]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
--最后还要提的一点是,Lua中 switch 语句的缺失,用 if -elseif- else 语句代替的话,显得非常臃肿,还有其他的一些实现方案。笔者在网上麦子加菲童鞋的博客中找到一种Lua中代替 switch 语句非常优雅的方案。下面贴出麦子加菲原代码:  
--Switch语句的替代语法(所有替代方案中觉得最好,最简洁,最高效,最能体现Lua特点的一种方案)  
action = {    
   [1] = function (x) print(x) end,    
   [2] = function (x) print( 2 * x ) end,    
   [ "nop" ] = function (x) print(math.random()) end,    
   [ "my name" ] = function (x) print( "fred" ) end,    
}    
    
while  true  do    
     key = getChar()    
     x = math.ramdon()    
     action[key](x)    
end

2、Lua中的函数

在Lua脚本中,函数是以function关键字开始,然后是函数名称,参数列表,最后以end关键字表示函数结束。需要注意的是,函数中的参数是局部变量,如果参数列表中存在(...)时,Lua内部将创建一个类型为table的局部变量arg,用来保存所有调用时传递的参数以及参数的个数(arg.n)。

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
function PrintTable (pTable)  
   for  index = 1, table.getn(pTable)  do   
       print( "pTable[" ,index, "] =" ,pTable[index])  
   end  
end  
   
gStringTable = { "hello" , "how" , "are" , "you" }  
PrintTable(gStringTable)  
--[[  
输出结果为:  
pTable[ 1   ] = hello  
pTable[ 2   ] = how  
pTable[ 3   ] = are  
pTable[ 4   ] = you  
--]]  
   
function PrintFriendInfo (name, gender, ...)   
   local friendInfoString = string.format( "name:%s  gender:%d" ,name,gender)  
   if  0 < arg.n then  
      for  index = 1, arg.n  do   
         friendInfoString = string.format( "%s otherInfo:%s" ,friendInfoString, arg[index])  
      end  
    end  
    print(friendInfoString)  
end  
   
   
PrintFriendInfo ( "eric" , 1,  "程序员" , "2b" , 50)  
   
  --输出结果为:  
-- name:eric  gender:1 otherInfo:程序员 otherInfo:2b otherInfo:50

Lua函数的返回值跟其他语言比较的话,特殊的是能够返回多个返回值。return之后,该Lua函数从Lua的堆栈里被清理。

1
2
3
4
5
6
7
8
9
10
function GetUserInfo ()  
     local name =  "eric"  
     local gender = 1  
     local hobby =  "动漫"  
   return  name, gender, hobby  
end  
   
print(GetUserInfo())  
   
--输出结果:eric 1   动漫


三、Lua中的库函数

在本文的最后,介绍一些Lua中常用的库函数。

1、数学库

math库的常用函数:三角函数math.sin、math.cos、取整函数math.floor、math.ceil、math.max、math.min、随机函数math.random、math.randomseed(os.time())、变量pi和huge。

2、I/O库

进行I/O操作前,必须先用io.open()函数打开一个文件。io.open()函数存在两个参数,一个是要打开的文件名,另一个是模式字符,类似"r"表示读取、“w”表示写入并同时删除文件原来内容,“a”表示追加,“b”表示打开二进制文件。该函数会返回一个表示文件的返回值,如果打开出错则返回nil,写入之前需要判断是否出错,比如:local file = assert(io.open(filename, “w”))..使用完毕后,调用io.close(file).或file:close()。

几个常用I/O函数:io.input ()、io.output ()、 io.read()、 io.write()。

1
2
3
4
5
local file =  assert (io.open(filename, “w”))  
if  file ~= nil then  
   file:write( "hello lua!!!!" )  --注意,等同于io.write( "hello lua!!!!" )  
   file:close()  --等同于io.close(file)  
end

3、调试库

  • debug.getinfo()函数,他的第一个参数 可以是一个函数或一个栈层。返回结果是一个table,其中包含了函数的定义位置、行号、函数类型、函数名称等信息。

  • debug.getlocal()函数检查函数任意局部变量,有两个参数,第一个是希望查询的函数栈层,另一个是变量的索引。

  • assert(trunk)() 函数,执行参数中代码块并在出错时提供报错功能。

1
2
3
4
5
a =  "hello world"  
b =  "print(a)"  
assert (loadstring(b))()  
--输出结果:  
hello world


4、几个处理Lua代码块的函数

  • loadstring(pString)()函数可以直接执行pString字符串组成的Lua代码,但不提供报错功能。

1
2
3
4
5
6
7
8
loadstring( "for index = 1, 4 do print(\"for cycle index =\",index) end" )()  
--[[  
输出结果  
for  cycle index =   1  
for  cycle index =   2  
for  cycle index =   3  
for  cycle index =   4  
--]]
  • dofile(filename)函数的功能是载入并立刻执行Lua脚本文件。可以用来载入定义函数的文件或者数据文件、或立即执行的Lua代码。dofile函数会将程序的执行目录作为当前目录。如果要载入程序执行目录的子目录里的文件,需要加上子目录的路径。

1
2
dofile( "/Users/ericli/WorkSpace/Lua语言/hellolua.lua" )  
--输出结果:Hello Lua!

本篇总结完毕,本篇只是总结了Lua的一些最基本的语法。至于Lua的更高级的内容,比如:协同程序、模块与包、Lua调用C代码、C++与Lua的整合等,还需要在以后的学习过程中深入。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值