lua基础学习

3 篇文章 0 订阅
1 篇文章 0 订阅
--lua开始学习了!!
--[[
	这个是学习注释 最好不要使用 _A 开头来定义变量
	学习下为知中lua编译器中自己写的编译器

print("hello lua");
--默认全局变量
str = "这个是全局的"
str = nil --删除
print(str)
--local 表示是局部的
local x = "局部的"
print(x)

--lua中的类型
print(type("hello"))
print(type(10.4*3))
print(type(print))
print(type(type))
print(type(true))
print(type(nil))
print(type(type(X)))



--tbale 测试 .. 为字符串连接 
tab1 = { key1 = "vall", key2 = "val2", "val3"}
for k, v in pairs(tab1) do
	print(k .. " - " .. v)
end


print("-----------")

tab1.key1 = nil --把key1删除
for k, v in pairs(tab1) do
	print(k .. " - " .. v)
end


-- 布尔类型的特别, 其中false和nil是假,其它均为真
-- 而返回类型也不一样
-- 返回 字符串而不是 true
print(true and "sfe")
print(0.2e-1)




--一块字符串

-- 取长度print(#"2 + 6")


a = {}
a["key"] = "value"
key = 10
a[key] = 22
a[key] = a[key] + 11
for k, v in pairs(a) do
	print(k .. " : " .. v)
end



-- 从1 开始为下标
tb1 = {"11", "22", "33", "4"}
for key, val in pairs(tb1) do
	print("Key", key, val)
end


-- 函数 *函数为第一类型*
function test(n)
	if 0 == n then
		return 1
	elseif n < 0 then
		return 0
	else
		return n * test(n - 1)
	end
end

print(test(5))
test2 = test
print(test2(-1))


function anonymous(tab, fun)
	for k, v in pairs(tab) do
		print(fun(k, v))
	end
end

tab = {key1 = "vall", key2 = "val2"}
--匿名函数的使用
anonymous(tab, function(key, val)
	return key .. " = " .. val
	end)



-- userData, 即可以把c/C++ 的任意类型数据存储到lua变量中调用

do
	local a = 6
	b = 6
	print(a,b)
end

tab = {"i", "2"}
tab.i = 12
print(tab.i)

-- 变参函数 注意 ipairs的使用
function average( ... )
	-- body
	res = 0
	local  arg = {...}
	for i,v in ipairs(arg) do
		res = res + v
	end

	print("Totil".. #arg .. "个")
	return res/#arg
end


print("平均值", average(10,5,3,4,5,6))



-- 不等于 ~= 幂运算 ^
-- 逻辑运算符 and, or, not

--string的使用,内置很多与CSting类似的全局函数
local strTest = "abcdEFG"
print(string.len(strTest))


-- 可从 负数开始索引数组
array = {"Lua", "TT"}
for i=0,2 do
	print(array[i])
end


-- 多维数组
array = {}
for i=1,3 do
	array[i] = {}
		for j=1,3 do
			array[i][j] = i*j
		end
end

for i=1,3 do
	for j=1,3 do
		print(array[i][j])
	end
end


tab = {"1", "2", "3"}
-- 类似于 std::sort函数
table.sort( tab, sortfunction )

-- concat,insert,remove 



tab = {"3", "2", "4"}
function sortFun(tag1, tag2)
	-- body
	return tag1 < tag2
end

table.sort(tab, sortFun)
print("排序后")
for k,v in ipairs(tab) do
	print(k,v)
end

-- 导入模块
local m = require("module1")
print(m.constant)

m.func3()

print(package.path)


-- 引入c库
local path = "/usr/local/lua/lib/libluasocket.so"
-- 或者 path = "C:\\windows\\luasocket.dll",这是 Window 平台下
local f = assert(loadlib(path, "luaopen_socket"))
f()  -- 真正打开库



-- 元素 Metatable
local mytable = setmetatable({key1 = "value1"}, {
  __index = function(mytable, key)
    if key == "key2" then
      return "metatablevalue"
    elseif key == "key3" then
    	return "3333"
    else
      return nil
    end
  end
})

-- 上述元表简化版,
mytable = setmetatable({key1 = "value1"}, { __index = { key2 = "metatablevalue" } })
print(mytable.key1,mytable.key2)

print(mytable.key2)
print(mytable.key1,mytable.key2)
print(mytable.key2)
print(mytable.key3)


-- 还有很多运算符 __add,__sub;等等,可以进行符号的重载。


-- __tostring 操作返回值__tostring
mytable = setmetatable({ 10, 20, 30 }, {
  __tostring = function(mytable)
    sum = 0
    for k, v in pairs(mytable) do
		sum = sum + v
	end
    return "表所有元素的和为 " .. sum
  end
})
print(mytable) -- 默认输出 地址

-- 协同 和多线程类似
-- 生产者消费者
local newProductor

function productor()
     local i = 0
     while true do
          i = i + 1
          send(i)     -- 将生产的物品发送给消费者
     end
end

function consumer()
     while true do
          local i = receive()     -- 从生产者那里得到物品
          print(i)
     end
end

function receive()
     local status, value = coroutine.resume(newProductor)
     return value
end

function send(x)
     coroutine.yield(x)     -- x表示需要发送的值,值返回以后,就挂起该协同程序
end

-- 启动程序
newProductor = coroutine.create(productor)
consumer()


-- 文件操作
file = io.open("module1.lua", "r")
io.input(file) -- 输出模式
print(io.read()) --读取一行
io.close(file)
file = io.open("module1.lua", "a")
io.output(file) -- 输入模式

io.write("\n")
io.write("-- 注释增加")
io.close(file)


-- 完全文件操作
file = io.open("module31.lua", "r")

print(file:read())

file:close();



--file = io.open("module331.lua", "a")
--file:write("\n--注释1111")
--file:close()



-- lua 类的实现
Shape = {area = 0}

function Shape:new(inData, side)
	-- body
	inData = inData or {}
	setmetatable(inData, self)
	self.__index = self
	side = side or 0
	self.area = side*side
	return inData
end

function Shape:printArea()
	-- body
	print("面积为 ", self.area)
end

Square = Shape:new()
function Square:new(inData, side)
	-- body
	inData = inData or Shape:new(inData, side)
	setmetatable(inData, self)
	self.__index = self
	return inData
end
function Square:printArea()
	-- body
	print("矩形面积 ", self.area)
end

myQuare = Square:new(nil, 10)
myQuare:printArea()

Rectangle = Shape:new()
function Rectangle:new(inData, lenght, width)
	inData = inData or Shape:new(inData)
	setmetatable(inData, self)
	self.__index = self
	self.area = lenght * width
	return inData
end


myData = Rectangle:new(nil, 10, 20)
myData:printArea()

--]]

-- 一块字符串
html = [[

<html>
<head></head>
<html>

]]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值