• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

http://www.runoob.com/lua/lua-basic-syntax.html

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

  1. Lua 是一个小巧的脚本语言。 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。Lua并没有提供强大的库,这是由它的定位决定的。Lua 有一个同时进行的JIT项目,提供在特定平台上的即时编译功能。

  2. Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,ini等文件格式,并且更容易理解和维护。 Lua由标准C编写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。一个完整的Lua解释器不过200k,在目前所有脚本引擎中,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字符串

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" , gStringTable[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)

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用MinGW编译Lua发布时间:2022-07-22
下一篇:
lua调用c函数发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap