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
tar zxf lua-5.2.3.tar.gz
cd lua-5.2.3
make linux test
For Mac OS X,use make macosx test.

以下内容参考http://www.dcc.ufrj.br/~fabiom/lua/01GettingStarted.pdf

(2)hello world

在控制台:

lua

io.write("hello world\n")

在文件:

vi defs.lua

function fact(n)
  if n < 2 then
    return 1
  else
    return n * fact(n - 1)
  end
end
lua

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)数据结构
  • table代表一切
>tab={}
>tab["x"]=5
>print(tab["x"])
5
  • arrays是下标从1开始的连续的integer值
local a={}
for i=1,6 do
  a[i]=math.random(10)
end
  • 数组长度
#a
添加:a[#a+1]=math.random(10)
删除:a[#a]=nil
  • 插入、删除、排序
> a = { 1,2,3,4,5 }

> 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 }
  • 连接concatenatation
table.concat将string类型的数组连接到一起。
分隔符可选,默认""
function print_array(a)
    print("{" .. table.concat(a,",") .. "}")
end 
习惯将string数组存为buffer,使用table.contact方法从buffer中获取单独的string
  • 使用ipairs迭代

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)

  • 矩阵matrices

local mt = {}
for i = 1,3 do
  mt[i] = {}
  for j = 1,5 do
    mt[i][j] = 0 
  end
end

  • Records(数据结构?)
table,keys是合法的标识符。table中,record的字段可以是key/value对

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"])

  • 集合sets

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 }
  • 使用ipairs迭代

local tab = { x = 5,y = 3,"foo" }
for k,v in pairs(tab) do
  print(tostring(k) .. " = " .. tostring(v))
end
pairs不是有序迭代,可用于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。
  • In your Eclipse 4.3+ go to Help > Install New Software...
  • Add the following repository:
    • http://download.eclipse.org/ldt/releases/milestones/
  • Now you can install the Lua Development Tools.

效果如下:
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

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...