Lua语法学习笔记
来源:互联网 发布:linux get命令 ftp 编辑:程序博客网 时间:2024/06/05 21:49
- 注释符–
- nil 表示变量还没有赋值。如果给一个变量赋值为nil,表示删除该变量。
- Number:Lua中数字只有双精度类型,不区分浮点和整型。
- 局部变量用local声明,Lua默认变量是全局的。
- 不等于~=
while和repeat
indx = 1while indx < 10 do indx = indx + 1endrepeat indx = indx + 1until indx > 10
for语句:Lua中的for语句循环次数在第一次就确定了,后面不改变
for indx = 1,10 do print(indx)endfor indx = 1,10,-1 do print(indx)end
函数
function SetName(myString)endfunction 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]); endendfunction 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 myValueenda = 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,myTotalenda,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 myValueend
- 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-1234a = "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) endend
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)endfor 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
0 0
- Lua语法学习笔记
- [LUA学习笔记04]Lua语法--数据类型
- lua学习笔记4之语法
- lua学习笔记之基本语法
- Lua语法学习笔记(一)
- Lua语法学习笔记(二)
- Lua学习笔记(2):基本语法
- Lua学习笔记(1)-基础知识、表达式语法
- lua学习笔记(1) --Lua基础语法
- Lua学习笔记之lua基本语法DAY1
- 【Lua】语法笔记#1
- 【Lua】语法笔记#2
- Lua语法学习
- Lua语法学习
- lua语法学习
- lua语法学习1
- lua语法学习2
- Lua 基本语法学习
- C++ #include—尖括号和双引号的区别
- python可视化编辑器Sublime Text 2
- Python的栈
- sicily 1154. Easy sort
- 高手复盘手记:如何判断一个题材或者利好到底有多大
- Lua语法学习笔记
- 非对称加解密——RSA加密、解密以及数字签名
- Hbase 启动失败htrace
- 16年1月2日记录:新站收录的问题探讨与博客群的维护问题探讨
- css中position定位
- JS入门总结
- 动态库的加载
- 如何取得从1970年1月1日0时0分0秒到现在的毫秒数?
- socket ---> server and client