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

--if 语句结构,如下实例:
gTable = {"hello", }
if nil ~= gTable[] and "hello" == gTable[] then
print("gTable[1] is" , gStringTable[])
elseif == gTable[] then
print("gTable[2] is", gTable[])
else
print("unkown gTable element")
end
--while 和repeat循环语句结构,while先判断条件,如果true才执行代码块(有可能跳过该代码块);repeat则是在最后判断条件,保证代码块至少执行一次。
gTable = {,,,,,,,,,}
index =
while gTable[index] < do
print("while gTable[",index,"] is ",gTable[index])
index = index + -- 注意,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 -
until index <
--[[
输出结果如下:
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
--]] --for循环结构,for循环结构具有三个参数,初始值,结束值,每个循环增加值。
for index = , 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 = , , - 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
--]] --break关键字可以使循环强制退出,Lua中没有continue关键字,需要通过其他方式实现continue关键字,比如if-else语句。或者通过网络下载Lua的continue关键字补丁安装来解决该问题 for index = , , do
if index > and index < then --用if-else语句实现continue关键字的功能
print("continue!!!!! index=",index)
else
if index > and index < 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
--]] --最后还要提的一点是,Lua中switch语句的缺失,用if-elseif-else语句代替的话,显得非常臃肿,还有其他的一些实现方案。笔者在网上麦子加菲童鞋的博客中找到一种Lua中代替switch语句非常优雅的方案。下面贴出麦子加菲原代码:
--Switch语句的替代语法(所有替代方案中觉得最好,最简洁,最高效,最能体现Lua特点的一种方案)
action = {
[] = function (x) print(x) end,
[] = function (x) print( * 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 、Lua中的函数
在Lua脚本中, 函数是以function关键字开始,然后是函数名称,参数列表,最后以end关键字表示函数结束 。需要注意的是, 函数中的参数是局部变量,如果参数列表中存在(...)时,Lua内部将创建一个类型为table的局部变量arg,用来保存所有调用时传递的参数以及参数的个数(arg.n) 。 function PrintTable (pTable)
for index = , 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 < arg.n then
for index = , arg.n do
friendInfoString = string.format("%s otherInfo:%s",friendInfoString, arg[index])
end
end
print(friendInfoString)
end PrintFriendInfo ("eric", , "程序员","2b", ) --输出结果为:
-- name:eric gender:1 otherInfo:程序员 otherInfo:2b otherInfo:50
     Lua函数的返回值跟其他语言比较的话,特殊的是能够返回多个返回值。return之后,该Lua函数从Lua的堆栈里被清理。
function GetUserInfo ()
local name = "eric"
local gender =
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() 。
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)() 函数,执行参数中代码块并在出错时提供报错功能。

a = "hello world"
b = "print(a)"
assert(loadstring(b))()
--输出结果:
hello world

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

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

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 函数会将程序的 执行目录作为当前目录。如果要载入程序执行目录的子目录里的文件,需要加上子目录的路径。

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

Lua中的常用语句结构以及函数的更多相关文章

  1. Lua 中的string库(字符串函数库)总结

    (字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...

  2. Lua中的常用函数库汇总

    lua库函数 这些函数都是Lua编程语言的一部分, 点击这里了解更多. assert(value) - 检查一个值是否为非nil, 若不是则(如果在wow.exe打开调试命令)显示对话框以及输出错误调 ...

  3. python 中一些常用的内置函数

    一.常用内置函数 abs(x) 返回绝对值,参数为int float,非字符只能num all(iterable) 如果迭代对象里面的所有值都为真就返回True.all([1, 2, -7]) --- ...

  4. 【Socket】Socket网络编程常用的结构及函数小结

    名词解析 IP地址的作用是标示计算机的网卡地址,每台计算机都有一个IP地址: 端口是指计算机中为了标示在计算机中访问网络的不同程序而设的编号,并不是网卡接线的端口,而是不同程序的逻辑编号,并不是实际存 ...

  5. lua中string常用api

    local a="abcdefgbbb" string.sub(a,1,3) 字符串截取 返回截取的字符串           print(string.sub(a,1,3))   ...

  6. PHP中一些常用的安全类函数

      (1) htmlspecialchars() 表单验证(验证表单中的数据是否为空以及提交的数据是否合法) htmlspecialchars() //该函数将预定义的字符转化为html实体,预定义的 ...

  7. oracle中的常用语句

    1:查看当前用户的缺省表空间 SELECT USERNAME, DEFAULT_TABLESPACE FROM USER_USERS; 2:查看当前用户的角色 SELECT * FROM USER_R ...

  8. sql server 查询日期中的常用语句, 例如本周第一天, 年内的第几周,有用

    --本周第一天    SELECT DATEADD(Day,1-(DATEPART(Weekday,getdate())+@@DATEFIRST-1)%7,getdate())   --or    s ...

  9. random模块中最常用的几个函数

    转自:http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html 随机整数:>>> import random> ...

随机推荐

  1. C经典之9-system,if,do,while---ShinePans

    #include <stdio.h> #include <conio.h> #include <stdlib.h> //system(); 这个指令须要用到此头文件 ...

  2. 【ichartjs】爬取理想论坛前30页帖子获得每个子贴的发帖时间,总计83767条数据进行统计,生成统计图表

    统计数据如下: {': 2451} 图形化后效果如下: 源码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//E ...

  3. VS中的代码拖放

    平时数据操作中,我们经常在选择文件,由于数据太多.数据目录太深,选择数据文件到TextBox中非常不方便,于是就想让控件支持拖放数据,很多软件都支持这个功能,非常实用. 在ArcGIS10.x开发平台 ...

  4. SQLServer 之 聚合函数

    一.聚合函数介绍 1.聚合函数最常用的: (1) COUNT:求个数 count函数用于计算满足条件的数据项数,返回int数据类型的值. [1] 语法结构:COUNT( {[[ all | disti ...

  5. hadoop,hbase,hive安装全记录(转)

    http://wenku.baidu.com/view/5eb3b4a6f90f76c661371abe.html 或http://blog.csdn.net/chengweipeng123/arti ...

  6. Nginx如何保留真实IP和获取前端IP

    原理: squid,varnish以及nginx等,在做反向代理的时候,因为要代替客户端去访问服务器,所以,当请求包经过反向代理后,在代理服务器这里这个IP数据包的IP包头做了修改,最终后端web服务 ...

  7. smartsvn9破解及license文件

    第一步:去官网下载自己系统smartsvn版本文件 下载地址:http://www.smartsvn.com/download 第二步:破解 (1) 将文件解压到系统路径:/opt/smartsvn ...

  8. oracle的check约束

    check约束是指检查性约束,使用check约束时.将对输入的每个数据进行检查,仅仅有符合条件的记录才会被保存到表中,从而保证了数据的有效性和完整性.      check约束既有下面的四个特点: 在 ...

  9. Android酷炫加载进度动画

    概述 本自定义动画进度酷炫View,是加载进度动画的自定义View,继承于ImageView来实现,主要实现蒙层加载进度的加载进度效果. 支持水平左右加载和垂直上下加载四个方向,同时也支持自定义蒙层进 ...

  10. 优化技术之Android高效开发

    基于Android平台的设备一定是嵌入式设备. 两个原则判断一个系统是否合理:不要做不必要做的事情:尽可能地节省内存的使用. 1. 尽量避免创建对象Object 2. 使用自身方法 3. 使用虚拟优于 ...