Lua语法学习笔记
2016-01-02 21:07
369 查看
注释符–
nil 表示变量还没有赋值。如果给一个变量赋值为nil,表示删除该变量。
Number:Lua中数字只有双精度类型,不区分浮点和整型。
局部变量用local声明,Lua默认变量是全局的。
不等于~=
while和repeat
for语句:Lua中的for语句循环次数在第一次就确定了,后面不改变
函数
返回值return
assert函数
输出:
helloworld
math.floor:向下取整,四省五入的话先加0.5在向下取整
math.random()
math.min():判断table里的数据最小值,与loadstring配合
tonumber()
tostring()
string.char(10)换行
string.len()
string.sub(myString,start,end)截取子串,start可以为负数,从末尾开始截取string.sub(myString,start)
string.format(“%d..”,int,..)
string.find(myString,”…”)
eg:
myString = “My name is John Smith”
sStart,sEnd = string.find(myString,”John”)
print(sStart,sEnd)–12,15
字符和格式:
table数据结构
table.getn()
table.sort()
I/O基础
nil 表示变量还没有赋值。如果给一个变量赋值为nil,表示删除该变量。
Number:Lua中数字只有双精度类型,不区分浮点和整型。
局部变量用local声明,Lua默认变量是全局的。
不等于~=
while和repeat
indx = 1 while indx < 10 do indx = indx + 1 end repeat indx = indx + 1 until indx > 10
for语句:Lua中的for语句循环次数在第一次就确定了,后面不改变
for indx = 1,10 do print(indx) end for indx = 1,10,-1 do print(indx) end
函数
function SetName(myString) end function SetName(...) if arg.n >0 then for indx = 1,arg.n do local myString = string.format("%s%d","Argument ",indx,":") end else print(myString,arg[indx]); end end function Multiply(val1,val2,...) local myString if arg.n == 0 then myString = string.format("%d%s%d%s%d",val1,"*",val2,"=",val1*val2) else local val3 = val1*val2*arg[1] myString = tring.format("%d%s%d%s%d%s%d",val1,"*",val2,"*",arg[1],"=",val3) end print(myString) end
返回值return
function TimesTwo(myValue) myValue = myValue*2 return myValue end a = 24 + TimesTwo(12) print(a) function ThreeDice() d1 = math.random(1,6) d2 = math.random(1,6) d3 = math.random(1,6) myTotal = d1 + d2 +d3 return d1,d2,d3,myTotal end a,b,c,d = ThreeDice()
assert函数
a = "hello world" b = "print(a)" assert(loadstring(b))()
输出:
helloworld
math.floor:向下取整,四省五入的话先加0.5在向下取整
math.random()
myDie = math.random(1,6) --设置随机数发生种子 \math.randomseed(os.date(%d%H%M%S))
math.min():判断table里的数据最小值,与loadstring配合
myTable = {7,1,13,6,89,4,56,2,54,6} function GetMin(theTable) myString = "myValue = math.min(" for index,value in ipairs(theTable) do myString = string.format("%s%d%s", myString, value, ",") end --remove final comma myString = string.sub (myString, 1, string.len(myString) - 1) myString = string.format("%s%s", myString, ")") loadstring(myString)() --run the chunk print(myString) -- see the string print(myValue) --see the result return myValue end
tonumber()
tostring()
string.char(10)换行
string.len()
string.sub(myString,start,end)截取子串,start可以为负数,从末尾开始截取string.sub(myString,start)
string.format(“%d..”,int,..)
string.find(myString,”…”)
eg:
myString = “My name is John Smith”
sStart,sEnd = string.find(myString,”John”)
print(sStart,sEnd)–12,15
字符和格式:
myString = "The price is $17.50." filter = "$%d%d.%d%d" print(string.sub(myString,string.find(myString,filter))) --$17.50
string.gsub(sourceString,pattern,replacementString) --返回一个字符串,sourceString字符中,满足pattern格式的字符都会被替换成replacementString参数的值 eg:
myString = "My name is John Smith. My phone is 555-3257" newString = string.gsub(myString,"%d","*") --My name is John Smith. My phone is ***-*** a = "(309) 555-1234" print(string.gsub(a,"%(%d%d%d%)","(781)")) --(781) 555-1234 a = "happy, hello, home, hot, hudson" print(string.gsub(a,"h%a+","An H world!",2)) --An H world!, An H world!, home, hot, hudson --查找以h开头的字符,%a+表示任意长度的字母,并在遇到空格和标点符号时为止,最后的参数2表示只替换最先遇到的两个 string.gfind(string,pattern)注意是遍历字符串,只要满足指定格式就返回该子串 a = "This is my rather long string." for v in string.gfind(a,"%a+") do print(v) end --This is my rather long string
table数据结构
table.getn()
table.sort()
--sort有可选的参数, 此参数是一个外部函数, 可以用来自定义sort函数的排序标准.此函数应满足以下条件: --接受两个参数(依次为a, b), 并返回一个布尔型的值, 当a应该排在b前面时, 返回true, 反之返回false. --下面是一个逆序排序,当direction=1时逆序 function Sort(theTable, direction) if direction ~= 1 then table.sort(theTable) else function Reverse(a, b) if a < b then return false else return true end end table.sort(theTable, Reverse) end end
table.insert(myTable,position,value)默认是末尾 table.remove(myTable,position) 默认是末尾,且返回去除值 多维table
widget = {} widget.name = {} widget.cost = {} widget.name[1] = "Can opener" widget.cost[1] = "$12.75" widget.name[2] = "Scissors" widget.cost[2] = "$8.99"
pairs():遍历table中的每一个元素
myName = {"Fred","Ethel","lucy","Ricky"} for index,value in pairs(myName) do print(index,value) end for index = 1,table.getn(myNames) do print(index,myName[index]) end
I/O基础
利用io可以生成可运行的lua文件,可以用于游戏数据或者游戏进度的保存
myFile = io.open("xxx.lua","w")--文件不存在则新建 w代表写入 if myFile ~= nil then myFile:write("--Test lua file") myFile:write(string.char(10))--换行 myFile:write(string.format("%s%s","--File created on:",os.date())) myFile::write("print(\"helloworld!\")") io.close(myFile) end
相关文章推荐
- lua模块的几种实现方式
- lua脚本语言的学习-----------------带参数的lua函数的传递
- lua脚本语言的学习-----------------如何实现c++无参数的函数在lua中调用
- lua脚本语言的学习-----------------VS2013如何编译lua脚本的三方库
- ngx_lua配置及应用
- 【转】说下lua使用场景
- ngx_lua应用最佳实践
- Lua脚本
- Lua基础之字符串(string)
- Lua基础之字符串(string)
- evaluatedOK == JS_FALSE 的问题
- OpenResty如何在一个lua文件调用另一个lua文件的函数
- angluar js学习网站
- Lua 中改变console的输出字体颜色
- 在Windows visual studio环境下编译Lua
- another lua vm
- Lua封装延时执行函数
- Lua封装将数字转换成万显示
- Lua封装打印Table函数
- Lua格式化字符串中的空格