标签:lua
lua基础
1.开发环境IDE及语法高亮
mac上安装lua
brew install lua
(1)sublime Text 2
新建lua.sublime-build的规则
(mac)
{
"cmd":["/usr/local/bin/lua","$file"],
"file_regex":"^(...*?):([0-9]*):?([0-9]*)",
"selector":"source.lua"
}
(windows)
{
"cmd":["c://Program file/sublime text 2/lua.exe","$file"],
}
运行时,直接按ctr+b,就可以得出结果
2.调试断点
debug:
mac上出现了点问题
Lua Remote
debugger
Run the program you wish to debug
调试按钮不可用
windowns的可以试试,SubDebug+mobdebug
3.定位crash是本身支持的
出错后悔显示错在第几行
4.语法
变量
lua数字只有double型
num =
1024
num =
3.0
num =
3.1416
num =
314.16e-2
num =
0.31416E1
num =
0xff
num =
0x56
lua中的变量如果没有特殊说明,全是全局变量,那怕是语句块或是函数里。变量前加local关键字的是局部变量。
theGlobalVar
=
50
local theLocalVar =
"local variable"
Lua没有++或是+=这样的操作
while循环
sum =
0
num =
1
while
num <=
100
do
sum = sum + num
num = num +
1
end
print("sum =",sum)
if-else分支
if
age ==
40 and sex =="Male"
then
print("男人四十一枝花")
elseif age >
60 and sex ~="Female"
then
print("old man without country!")
elseif age <
20 then
io.write("too young, too naive!\n")
else
local age = io.read()
print("Your age is "..age)
end
sum =
0
for
i =
1,
100
do
sum = sum + i
end
until循环
sum =
2
repeat
sum = sum ^
2 --幂操作
print(sum)
until sum >1000
函数
function
fib(n)
if
n <
2
then
return
1
end
return
fib(n -
2)
+ fib(n -
1)
end
闭包
function
newCounter()
local i =
0
return
function()
i = i +
1
return i
end
end
c1 = newCounter()
print(c1()) -->
1
print(c1()) -->
2
函数的返回值
function
getUserInfo(id)
print(id)
return
"haoel",
37
end
name, age = getUserInfo()
assert
assert(money >
0,"need up to 0")
pairs:遍历表的所有键名和键值
t = {one =
"a",two =
"b",three =
"c"}
for
k,v in
pairs(t) do
print(k,v)
end
rawequal:判断两个值是否相等
print(rawequal(1,2))
type:返回参数的类型
print(type("hello"))
5.系统函数库
table表函数
例如数组
table.concat(连接字符串)
table.insert(插入元素)
table.remove(删除元素)
table.sort(排序)
string
math
file
os系统函数
os.clock() 返回使用cpu的总时长
lua基础
标签:lua
原文地址:http://blog.csdn.net/rainlesvio/article/details/45536627