ulua学习笔记1

5.3k 词

本文为bbbbbbion(可以叫我六饼)原创总结,如有疏漏请各位拍砖留言。转载请尊重原作者成果,保留出处。

这一系列用于记录笔者学习ulua热更新的过程。首先来看Windows下lua环境的搭建。

环境:lua for windows (lfW)
主页:http://luaforwindows.luaforge.net/

lua for windows是一整套Lua的开发环境,主要包括:

  • Lua Interpreter(Lua解释器)

  • Lua Reference Manual(Lua参考手册)

  • Quick Lua Tour(Lua快速入门)

  • Examples(Lua范例)

  • Librarier with documentation(一些Lua库和文档)

  • SciTE(一款多用途编辑器,已经对Lua做了特殊的设置)

上述提到的主页上可以下载lfW(需要翻~墙),最新版本为:5.1.4-46。一路默认安装,最后一步会询问是否使用SciTE默认的“黑色”风格,一般建议勾选。

使用SciTE:打开SciTE,新建文件,输入print(“hello world”),然后保存为test.lua,注意文件名后缀一定要带上,且为.lua,否则无法正确运行。
然后按F5,结果如图:
"Lua Hello world"

基础知识

基础知识以5.1.4-46这个环境下为例。

1,注释
–单行注释
–[[多行注释,
多行注释]]

2,变量
—-代码—-
a=1
b=”abc”
c={}
d=print

print(type(a))
print(type(b))
print(type(c))
print(type(d))

—-输出—-
number
string
table
function

3,变量名称
字符、数字、下划线组成,且不能以数字开头。
以下划线开头的变量通常用于特殊的值,如_VERSION,输出结果为当前Lua的版本。所以变量一般也不以下划线开头。

—-代码—-
print(_VERSIONG)
—-结果—-
Lua 5.1

4,大小写敏感
—-代码—-
ab=1
Ab=2
AB=3
print(ab,Ab,AB)

—-输出—-
1 2 3

5,关键字
Lua的关键字,如:and,break,do,else,elseif,for,function,if,local,nil,not,or,end,false,in,repeat,return,then,true,until,while。
关键字不能用于变量名。

6,字符串
单行字符串可以用””或’’修饰,若字符串中包含”或’字符,需要使用转义符
多行字符串用[[XXXXXX]]

7,赋值
Lua支持多重赋值。
—-代码—-
a,b,c,d,e=1,2,”three”,”four”,5
print(a,b,c,d,e)
a,b,c,d,e=e,d,c,b,a
print(a,b,c,d,e)

—-输出—-
1 2 three four 5
5 four three 2 1

8,number类型
lua的数字类型都是默认double型,不用担心其精度和处理速度的问题。
—-代码—-
a,b,c,d,e=1,1.123,1E9,-123,.0008
print(“a=”..a,”b=”..b,”c=”..c,”d=”..d,”e=”..e) –..是字符串连接符

—-输出—-
a=1 b=1.123 c=1000000000 d=-123 e=0.0008

9,打印
print “Hello world!”
print ‘Hello world!’
print(“Hello world!”)
print(‘Hello world!’)

print打印后会换行。

io.write “Hello world!”
io.write ‘Hello world!’
io.write(“Hello world!”)
io.write(‘Hello world!’)

io打印后不换行。

10,Tables
—-代码,简单表的创建—-
a={} –{}创建一个空的表
b={1,2,3} –创建一个表,包含数值1,2,3
c={“a”,”b”,”c”} –创建一个表包含字符串 a,b,c
print(a,b,c) –表不能直接打印,因此结果很诡异

—-输出—-
table:002DBA80 table:002DBAD0 table:002dbaf8

—-代码,访问表—-
address={} –一个空表address
address.Street=”WuHan”
address.Age=”100”
address.Country=”China”

print(address.Street,address[“Age”],address[“Country”])

—-输出—-
Wuhan 100 China

11,if语句
—-代码,简单if语句—-

a=1
if a==1 then
    print("a is one")
end

—-输出—-
a is one

—-代码,if else—-

b="happy"
if b=="sad" then
    print("b is sad")
else
    print("b is not sad")
end

—-输出—-
b is not sad

—-代码,if elseif else—-

c=3
if c==1 then
    print("c is 1")
elseif c==2 then
    print("c is 2")
else
    print("c is not 1 or 2,c is"..tostring(c)) --也可以用print("c is not 1 or 2,c is"..c)
end

—-输出—-
c is not 1 or 2,c is 3

12,while语句
—-代码—-

a=1
while a~=5 do --Lua use ~= to mean not equal
    a=a+1
    io.write(a.." ")
end

—-输出—-
2 3 4 5

13,repeat until语句
—-代码—-

a=0
repeat 
    a=a+1
    print(a)
until a==5

—-输出—-
1
2
3
4
5

可以发现,repeat until语句不需要使用end

14,for语句
—-代码—-

for a=1,4           --从1输出4,每次递增1
    do io.write(a.." ")
end

print()

for a=1,6,3
do io.write(a…" ")
end

for key,value in pairs({1,21,3,4})
do print(key,value)
end

—-输出—-
1 2 3 4
1 4
1 1
2 21
3 3
4 4

15,打印表
—-代码—-

a={1,2,3,4,"five","elephant","mouse"}
for i,v in ipairs(a)
    do print(i,v)
end

—-输出—-
1 1
2 2
3 3
4 4
5 five
6 elephant
7 mouse

16,break语句
–break用于跳出循环
—-代码—-

a=0
while true do
    a=a+1
    if    a==10 then
        break
    end
end
print(a)

—-输出—-
10

17,函数
–定义一个无参数,无返回值的函数。

function myFirstLuaFunction()
    print("My first lua function was called")
end

–定义一个有返回值的函数
function mySecondLuaFunction()
return “string from my second function”
end
–第一有参数,有返回值的函数
function myThirdLuaFunction(a,b,c)
return a,b,c,“a string”,1,true
end

–调用函数
myFirstLuaFunction()
mySecondLuaFunction()
myThirdLuaFunction(1,2,”three”)

18,变量作用域
默认变量的作用域为全局的,只有前面加了local标识的变量才是局部的。

19,格式化打印
—-代码—-

function printf(fmt,...)
    io.write(string.format(fmt,...))
end
printf("Hello %s from %s on %sn",os.getenv "USER" or "there",_VERSION,os.date())

—-输出—-
Hello there from Lua 5.1 on 09/03/15 16:55:59

20,Lua库

  • math库:
    math.abs,math.acos,math.cos,math.deg,math.exp,math.ceil,math.floor,math.fmod,math.log,math.log10,math.min,math.max,math.modf,math.pi,math.pow,math.rad,math.random,math.sqrt,math.randomseed等。

  • string库:
    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

  • table库:
    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

—-输出—-
1 4
2 3
3 2

21,标准库-input/output
io.close,io.flush,io.input,io.lines,io.open,io.output,io.popen,io.read,io.stderr,io.stdout,io.tmpfile,io.type,io.write,
file:close,file:flush,file:line,file:read,file:seek,file:setvbuf,file:write

22,标准库-os相关
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())
—-输出—-
09/03/15 22:48:22

23,外部库
–Lua通过使用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()

补充:

  • 1,pairs和ipairs
    ipairs(t):
    for i,v in ipairs(t) do body end
    will iterate over the pairs(1,t[1]),(2,t[2]),…,up to the first integer key absent from the table.(特别要注意这里index是从1开始的,也即lua的下标是从1开始的)

    pairs(t):
    for k,v in pairs(t) do body end
    will iterate over all key-value pairs of table t.

  • 2,null在lua中用nil表示,对于布尔类型只有nil和false是false,数字0、空字符串都是true

  • 3,lua没有++,+=这些操作符
  • 4,条件表达式中的与、或、非用and、or、not关键字,不可以使用&&、||、!

  • 5,lua中的全局变量实质上被存储在一个叫”_G”的Table中了,比如我们有个全局变量abc,则可以通过_G.abc或者_G[“abc”]来访问这个全局变量。

参考:
Lua5.1在线手册:http://book.luaer.cn/
云风翻译Lua手册:http://www.codingnow.com/2000/download/lua_manual.html