lua基础看这一篇基本就够了,文章有点长,需要耐心慢慢看
基于《lua程序设计2nd》
1.开始
以Lua5.1版本为例
星期几(
周日→周六 1-7) 美国人以周日为一周的开始,所以c语言以0表示周日,而lua是从1开始的,所以1表示周日
local date = os.date("*t")
print(date.wday)
从一个简单的阶乘开始
print(_VERSION) --输出Lua的版本
function fact(n)
if n==0 then
return 1
else
return n*fact(n-1)
end
end
print("Enter a number:")
a=io.read("*number") --读取一个数字
print(fact(a))
Lua关键字21个
and break do else elseif
end false for function if
in local nil not or
repeat return then true until
while
注释
--单行注释
--[[
多行注释
--]]
多行注释要启用很简单
---[[
--]]
全局变量
a=10000
print(a)
a=nil--删除全局变量
print(a)
UNIX系统中写lua文件
#!/usr/local/bin/lua
or
#!/usr/bin/env lua
Lua解释器
$ lua --help
usage: lua [options] [script [args]].
Available options are:
-e stat execute string 'stat'
-l name require library 'name'
-i enter interactive mode after executing 'script'
-v show version information
-- stop handling options
- execute stdin and stop handling options
-e
$ lua -e "print(math.pi)"
3.1415926535898
$ lua -i -e "_PROMPT=' lua> '" --UNIX中可以这样来改变命令提示符
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
lua>
-----------------------------------------------------------------------------------
2.类型与值
8种基础类型
nil、boolean、number、string、userdata、function、thread、table
1.nil
只有一个值nil
2.boolean
有两个值false、true
只有false、nil为假
0、空串为真
3.number
表示的是实数
4.string
lua中string不可改
eg:
a="one string"
b=string.gsub(a,"one","another")
print(a,b)
长字符串的处理
--如果这中间有字符]]这种之类的要在[[中间加入=,且与]]中间加入=数量相同
--在[[ ]]之中--失效
page=[==[
<html>
<head>
<title>long string handle</title>
</head>
<body>
<a href="http://www.google.com">Google</a>
<br>
what a fuck string:
<font color=#FF0000>a=b[c[i]]</font>
<br><br>
Attention two ]] here!So need to add = between ]],like following
<br>
[=[
<br>
a=b[c[i]], now it can show normally.
<br>
]=]
</body>
</html>
]==]
--写文件
file=io.open("link.html", "w")
file:write(page)
file:close()
--直接输出
io.write(page)
..
字符串连接操作符
eg:
print(10 .. 20)
类型转换
但一般还是用tonumber() tostring()
来显式转换
eg1:
line = io.read()
n = tonumber(line)
if n == nil then
error(line .. " is not a valid number")
else
print(n^3)
end
eg2:
print(tostring(10) == "10")-->true
print(10 .. "" == "10")-->true
length operator
字符串前置操作符 # 可获取该字符串的长度
eg:
a = "hello"
print(#a)-->5
print(#"good\0bye")-->8
5.table
table是一个关联数组,可以使用除了nil的值作为key
table是引用,local的临时表实际是修改到原来的table表
eg:
t = { [1] = 1, [2] = 2, }
do
local tmp = t
tmp[1] = 10086
end
for k, v in pairs(t) do
print(k .. " -> " .. v)
end
output:
1 -> 10086
2 -> 2
初始化空表再添加/添加式
eg:
t={}
t['x']=100
t[999]="abcd\tabcd"
for key,value in pairs(t) do
print(value .. '\t')
end
初始化数组/列表式
eg://初始化后以1-#days的index访问
days = {"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"}
days[1] == "Sunday"
初始化记录/记录式
为了表示一条记录,可将字段名作为索引
对于 t['x'] or t["x"]可以写成t.x
a={x=10,y=20} --相当于a.x=10; a.y=20
混合列表式/记录式
eg:
polyline={color="blue",thickness=2,npoints=4,
{x=0,y=0},
{x=-10,y=0},
{x=-10,y=1},
{x=0,y=1}
}
print(polyline.color) --blue
print(polyline[1].x .. '\t' .. polyline[1].y)
print(polyline[4].x .. '\t' .. polyline[4].y)
通用构造式
eg://在方括号中显式用表达式来初始化index值
opnames={
["+"]="add",
["-"]="sub",
["*"]="mul",
["/"]="div",
[1]=100
}
两个tips
1.可在最后一个元素后面写一个逗号
eg:
a={[1]="red", [2]="green", [3]="blue"
,
} --这样无需把最后一个元素特殊处理
2.可以把列表式与记录式明显地区分开
eg:
t={x=10,y=45; "one","two","three"}
惯用实例
print(a[#a]) --打印最后一个值