安装luaforwindows https://github.com/rjpcomputing/luaforwindows/releases,然后将lua添加进环境变量,并且使用SciTE就可以编辑并运行lua代码了。
Lua在线教程 http://lua-users.org/wiki/TutorialDirectory
其他的诸如手册和教程,也可以在开始菜单的Lua下面找到
- Hello World
print("Hello World")
- 分号可有可无
- 注释
--[[多行 注释]] -- 单行注释
- 变量声明不需要强类型,通过type关键字可以获取类型
a=1 b="abc" c={} d=print print(type(a)) print(type(b)) print(type(c)) print(type(d))
——– Output ——
number
string
table
function
- 变量名不要以下划线开头,如 _VERSION
- lua对变量名区分大小写
- 字符串可以用单引号,双引号,双括号来定义:
a="single 'quoted' string and double \"quoted\" string inside" b='single \'quoted\' string and double "quoted" string inside' c= [[ multiple line with 'single' and "double" quoted strings inside.]]
- 可以使用多重声明
a,b,c,d,e = 1, 2, "three", "four", 5
- 用一行代码进行变量值的互换
a,b=1,2 print(a,b) a,b=b,a print(a,b)
- 两点(..)可以用来连接字符串(或一个字符串和一个number)
a,b,c,d,e = 1, 1.123, 1E9, -123, .0008 print("a="..a, "b="..b, "c="..c, "d="..d, "e="..e)
- print可以打印
print "Hello from Lua!" print("Hello from Lua!")
- io.write方法不会换行,空参数的print方法可以换行
io.write("Hello from Lua!") io.write("Hello from Lua!") print() io.write("Hello from Lua!")
——– Output ——
Hello from Lua!Hello from Lua!
Hello from Lua!
- lua的Table数据结构,无法遍历打印Table的值,只会打印其地址
a={} -- {} creates an empty table b={1,2,3} -- creates a table containing numbers 1,2,3 c={"a","b","c"} -- creates a table containing strings a,b,c print(a,b,c) -- tables don't print directly, we'll get back to this!!
——– Output ——
table: 00A44DF0 table: 00A44FD0 table: 00A45098
- Table的更多使用
address={} -- empty address address.Street="Wyman Street" address.StreetNumber=360 address.AptNumber="2a" address.City="Watertown" address.State="Vermont" address.Country="USA" print(address.StreetNumber, address["AptNumber"])
——– Output ——
360 2a
- if elseif else end
a=1 if a==1 then print("a is one") end
b="happy" if b=="sad" then print("b is sad") else print("b is not sad") end
c=3 if c==1 then print("c is 1") elseif c==2 then print("c is 2") else print("c isn't 1 or 2, c is "..tostring(c)) end
- or的使用,相当于判断nil
a=0; a=b or 1; print(a);
——– Output ——
1
- and和or的使用,相当于c类语言的三元运算符 ? :
a=1 b=(a==1) and "one" or "not one" print(b)
——– Output ——
one
- while语句
a=1 while a~=5 do -- Lua uses ~= to mean not equal a=a+1 io.write(a.." ") end
——– Output ——
2 3 4 5
- repeat和until语句
a=0 repeat a=a+1 io.write(a) until a==5
——– Output ——
12345
- for语句
-- Count from 1 to 4 by 1. for a=1,4 do io.write(a) end print() -- Count from 1 to 6 by 3. for a=1,6,3 do io.write(a) end
——– Output ——
1234
14
- for语句遍历pairs
for key,value in pairs({1,2,3,4}) do print(key, value) end -------- Output ------
1 1
2 2
3 3
4 4
- 打印tables
a={1,2,3,4,"five","elephant", "mouse"} for i,v in pairs(a) do print(i,v) end -------- Output ------
1 1
2 2
3 3
4 4
5 five
6 elephant
7 mouse
- break语句来退出循环
a=0 while true do a=a+1 if a==10 then break end end print(a)
——– Output ——
10
- lua的方法,function
function myFirstLuaFunction() print("My first lua function was called") end myFirstLuaFunction()
——– Output ——
My first lua function was called
function mySecondLuaFunction() return "string from my second function" end a=mySecondLuaFunction("string") print(a)
——– Output ——
string from my second function
function myFirstLuaFunctionWithMultipleReturnValues(a,b,c) return a,b,c,"My first lua function with multiple return values", 1, true end a,b,c,d,e,f = myFirstLuaFunctionWithMultipleReturnValues(1,2,"three") print(a,b,c,d,e,f)
——– Output ——
1 2 three My first lua function with multiple return values 1 true
- 变量范围和function,以及local关键字,所有的变量默认都是全局变量
b="global" -- To make local variables you must put the keyword 'local' in front. function myfunc() local b=" local variable" a="global variable" print(a,b) end myfunc() print(a,b)
——– Output ——
global variable local variable
global variable global
- 格式化打印,实现printf
--这里的三个点...类似于c#的params--,即不定量参数 function printf(fmt, ...) io.write(string.format(fmt, ...)) end printf("Hello %s from %s on %s\n", os.getenv"USER" or "there", _VERSION, os.date())
——– Output ——
Hello there from Lua 5.1 on 05/03/17 16:47:50
- lua包含标准库和扩展库
- 标准库之math
-- Math functions: -- math.abs, math.acos, math.asin, math.atan, math.atan2, -- math.ceil, math.cos, math.cosh, math.deg, math.exp, math.floor, -- math.fmod, math.frexp, math.huge, math.ldexp, math.log, math.log10, -- math.max, math.min, math.modf, math.pi, math.pow, math.rad, -- math.random, math.randomseed, math.sin, math.sinh, math.sqrt, -- math.tan, math.tanh print(math.sqrt(9), math.pi)
——– Output ——
3 3.1415926535898
- 标准库之string
-- String functions: -- string.byte, string.char, string.dump, string.find, string.format, -- string.gfind, string.gsub, string.len, string.lower, string.match, -- string.rep, string.reverse, string.sub, string.upper print(string.upper("lower"),string.rep("a",5),string.find("abcde", "cd"))
——– Output ——
LOWER aaaaa 3 4
- 标准库之table
-- Table functions: -- table.concat, table.insert, table.maxn, table.remove, table.sort a={2} table.insert(a,3); table.insert(a,4); table.sort(a,function(v1,v2) return v1 > v2 end) for i,v in ipairs(a) do print(i,v) end
——– Output ——
1 4
2 3
3 2
- 标准库之input/output
-- IO functions: -- io.close , io.flush, io.input, io.lines, io.open, io.output, io.popen, -- io.read, io.stderr, io.stdin, io.stdout, io.tmpfile, io.type, io.write, -- file:close, file:flush, file:lines ,file:read, -- file:seek, file:setvbuf, file:write print(io.open("file doesn't exist", "r"))
——– Output ——
nil file doesn’t exist: No such file or directory 2
- 标准库之操作系统信息
-- OS functions: -- os.clock, os.date, os.difftime, os.execute, os.exit, os.getenv, -- os.remove, os.rename, os.setlocale, os.time, os.tmpname print(os.date())
——– Output ——
05/03/17 17:11:28
- 外部库,引用外部库需要用require关键字
require( "iuplua" ) ml = iup.multiline { expand="YES", value="Quit this multiline edit app to continue Tutorial!", border="YES" } dlg = iup.dialog{ml; title="IupMultiline", size="QUARTERxQUARTER",} dlg:show() print("Exit GUI app to continue!") iup.MainLoop()
——– Output ——
Exit GUI app to continue!