lua学习笔记
最近想搞torch,发现是lua写的。 lua是嘛玩意?学学看。 以下内容参考http://www.lua.org/start.html (1)到官网,先装上再说。 curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz ?也可以在浏览器直接输入http://www.lua.org/ftp/lua-5.2.3.tar.gz 以下内容参考http://www.dcc.ufrj.br/~fabiom/lua/01GettingStarted.pdf (2)hello world 在控制台: lua io.write("hello worldn") 在文件: vi defs.lua function fact(n) if n < 2 then return 1 ?else return n * fact(n - 1) end endlua dofile("defs.lua") print(fact(6)) (3)函数 通过命名空间区分函数 print和dofile都是builld-in函数 math函数,pi、sin、sqrt、random 日期os.date() 调用(运行)脚本dofile (4)变量 默认变量是全局的,默认值nil 变量无需变量,只需赋值然后使用 (5)注释 --我是单行注释 --[[ 我们是多行注释 --]] 以下内容参考http://www.dcc.ufrj.br/~fabiom/lua/04Functions.pdf (6)函数 local function local function max(a,b) return (a>b) and a or b end multiple result > s,e = string.find("hello Lua users","Lua") > print(s,e)? 7 9? 具体的使用方法,参考原博客。挺巧妙的 以下内容参考http://www.dcc.ufrj.br/~fabiom/lua/05DataStructures.pdf (7)数据结构
>tab["x"]=5 >print(tab["x"]) 5
for i=1,6 do ? a[i]=math.random(10) end
添加:a[#a+1]=math.random(10) 删除:a[#a]=nil
> table.insert(a,10) -- insert 10 in position 3 > print_array(a) ? { 1,10,5 } > table.remove(a,4)???? -- remove fourth element > print_array(a) ? { 1,5 } > a = { "Python","Lua","C","JavaScript","Java","Lisp" } > table.sort(a) ?> print_array(a) ?{ C,Java,JavaScript,Lisp,Lua,Python }
table.concat将string类型的数组连接到一起。
分隔符可选,默认"" function print_array(a) print("{" .. table.concat(a,",") .. "}") end习惯将string数组存为buffer,使用table.contact方法从buffer中获取单独的string
local a = { 1,5,7,9 } local sum = 0 for i,x in ipairs(a) do print("adding element " .. i) sum = sum + x end print("the sum is " .. sum)
local mt = {} for i = 1,3 do mt[i] = {} for j = 1,5 do mt[i][j] = 0 end end
point1 = { x = 10,y = 20 } point2 = { x = 50,y = 5 } line = { from = point1,to = point2,color = "blue" } line.color = "red" -- same as line["color"] = "red" print(line.from.x,line["color"])
table,keys是集合的元素,value是true。通过索引删除和添加元素 -- a set of four random integers from 1 to 10 local set = { [math.random(10)] = true,[math.random(10)] = true,[math.random(10)] = true }
local tab = { x = 5,y = 3,"foo" } for k,v in pairs(tab) do print(tostring(k) .. " = " .. tostring(v)) endpairs不是有序迭代,可用于set等;array须使用ipairs迭代 (8)复习一下上面的数据结构 >sunday = "monday"; monday = "sunday" >t = { sunday = "monday",[sunday] = monday } >print(t.sunday,t[sunday],t[t.sunday]) monday sunday sunday分析过程: (9)敲代码费劲?用IDE? 看这里http://www.eclipse.org/ldt/#installation。
效果如下:
local function main() io.write("hello,lua") t = {} s = "from=world,to=Lua" for k,v in string.gmatch(s,"(%w+)=(%w+)") do t[k] = v print(v) end a={} i=0 s=" 1 2 3 -9 5" for w in string.gmatch(s,"%d+") do print(w) a[i]=w i=i+1 end print(a) end main() (9)保存table到文件 function TableToString(t) local r="" for k,v in pairs(t) do r=r.." "..v --空格分隔 end return r end file = io.open("z_result.txt","w") a={1,-3,"3"} file:write(TableToString(a)) file:close()?1 2 -3 3 (10)从文件加载double数据 这个整死我了,感觉花了一辈子时间 function analys(line) for w in string.gmatch(line,"[^%s]+") do print(w.." "..tonumber(w).." ") end end function main() a = os.clock() file = io.open(filename,"r") for line in file:lines() do analys(line) end file.close() b = os.clock() print ("time: "..(b-a).."s") end main()数据格式示例: 0.278285 -5.63E-4? 0.156217 0.365401 -0.196637 2.75E-4 1.0 0.0 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |