您的位置:首页 > 编程语言 > Lua

lua笔记

2015-11-17 11:24 417 查看
print("hello world")

local function fact(n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end

local a = fact(5)
print(a)

print(b)

--。Lua中有8个基本类型分别为:nil、boolean、number、string、userdata、function、thread和table
print(type("hello world"))
print(type(4.6))
print(type(print))
print(type(type))
print(type(true))
print(type(nil))
print(type(type(x)))

testTable = {}
testTable = nil

function testNil()
if  testTable == nil then
print("nil is false")
else
print("nil is not false")
end
end
testNil()

--[[这些操作符返回结果为false或者true;==和~=比较两个值,如果两个值类型不同,
Lua认为两者不同;nil只和自己相等。Lua通过引用比较tables、userdata、functions。
也就是说当且仅当两者表示同一个对象时相等。]]
a = {}; a.x = 1; a.y = 0;
b = {}; b.x = 1; b.y = 0;
c = a;
function testEqual()
if a == b then
print("a == b")
elseif c == a then
print("a == c")
end
end
testEqual()

--[[逻辑运算符认为false和nil是假(false),其他为真,0也是true.
and和or的运算结果不是true和false,而是和它的两个操作数相关]]
print(4 and 5)
print(4 and false)
print(false and nil)
print(false or nil)
print(true or true)
x = x or 10
print(x)

--三元运算符,前提条件是b不为假
a = 1; b = true; c = 2;
print((a and b) or c)

--not的结果一直返回false或者true
print(not false)

--..字符串连接,如果操作数为数字,Lua将数字转成字符串。
print("hello" .. " world")
print(1 .. 2)

--对于table usedata,function,lua是作引用比较的;nil只与他自身相等
a = {}
a.x = 1; a.y = 2
b = {}
b.x = 1; b.y = 2
c = a
print(a == b)
print(a ~= b)
print(a == c)

--表的构造
local days = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"}
print(days[1])
local vec = {x = 1, y = 2, "sun"}
print(vec[1])
print(vec["x"])
print(vec.y)

--表索引
local a = {["x"]=3, ["y"]=4}
a["z"] = 5
a.w = 6
print(a.x)
print(a["y"])
print(a.z)
print(a.w)

--list风格初始化和record风格初始化是这种一般初始化的特例:
--{x=0, y=0} <--> {["x"]=0, ["y"]=0}
--{"red", "green", "blue"} <--> {[1]="red", [2]="green", [3]="blue"}

--链表
local single_list = nil
local function make_list(begin, count)
for i = 1, count, 1 do
single_list = {value = begin, next = single_list}
begin = begin + 1
end
end
make_list(1, 10)

local function print_list()
local temp_list = single_list;
while temp_list ~= nil do
print("list value:" .. temp_list.value)
temp_list = temp_list.next;
end
end
print_list()

function test_do()
--Lua可以对多个变量同时赋值,变量列表和值列表的各个元素用逗号分开,赋值语句右边的值会依次赋给左边的变量。
--a, b = 10, 2*x   <-->   a=10; b=2*x

--遇到赋值语句Lua会先计算右边所有的值然后再执行赋值操作,所以我们可以这样进行交换变量的值
do
x, y = y, x
end

--if
do
if true then

elseif false then

else

end
end

--while
do
while true do

end
end

--for
do
for i = 1, 10, 1 do
print(i)
end
end
end

--泛型for
function foreach()
local list = {"2", "3", "4"}
for i,v in ipairs(list) do
print("value:" .. v)
end

for i in pairs(list) do
print("kay:" .. i)
end
end
foreach()

--Lua语法要求break和return只能出现在block的结尾一句(也就是说:作为chunk的最后一句,或者在end之前,或者else前,或者until前)
--[[
function foo ()
return  --<< SYNTAX ERROR
-- 'return' is the last statement in the next block
do return end -- OK
...   -- statements not reached
end ]]

--可变参数:Lua函数可以接受可变数目的参数,和C语言类似在函数参数列表中使用三点(...)表示函数有可变的参数。Lua将函数的参数放在一个叫arg的表中,除了参数以外,arg表中还有一个域n表示参数的个数。
function pair_fun(a, b, ...)
for i, v in ipairs(arg) do
print("arg:" .. i .. " " .. v)
end
end
pair_fun(1,2,3)
pair_fun(1,2,3, 4)
pair_fun(1,2)

print(math.sin(1))
--闭包
function package()
local old_sin = math.sin
local k = math.pi/180
math.sin = function(x)
return old_sin(x*k)
end
end
package()
print(math.sin(1))

--非全局函数
lib = {}
lib.foo =
function(x, y)
return x+ y
end
print(lib.foo(1,1))

function lib.goo(x , y)--另一种语法方式
return x - y
end
print(lib.goo(1,2))

--函数定义的两种方式
local f = function()

end
function f()

end

--闭包函数调用
function list_iter(t)
local i = 0
local n = table.getn(t)
return function()
i = i + 1
if i <= n then
return t[i]
end
end
end
function use_iter()
local t = {10,20,30}
iter = list_iter(t)
while true do
local element = iter()
if element == nil then
break
end
print(element)
end
end
use_iter()

--loadstring
local f = loadstring("local a = 1; return a + 2")
print(f())

--assert
assert(true)

--error
local function foo()
--if false then
error()
debug.traceback()
--end
end
function process_call()
foo()
end
--process_call()

--多维数组
mt = {}
function create_mt()
for i = 1, 10 do
mt[i] = {}
for j = 1, 10 do
mt[i][j] = i * j
end
end
end
function print_mt()
for i = 1, table.getn(mt) do
for j, v in ipairs(mt[i]) do
print(i, j, v)
end
end
end
create_mt()
print_mt()

--可变参数
function add(...)
local s = 0
for i, v in ipairs{...} do
s = s + v
end
return s
end
print(add(1,2,3))

--元表
my_meta=
{
__add=function(op1, op2)
op = {}
op.x = op1.x + op2.x;
op.y = op1.y + op2.y;
return op
end
}
a={x=1, y=2}
setmetatable(a,my_meta)
b={x=3,y=4}
c=a+b
print(c.x,c.y)--输出4,6


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: