Lua简明示意

Lua

注释

-- 单行注释

--[[
	多行注释
--]]

关键词

以下列出了 Lua 的保留关键字。保留关键字不能作为常量或变量或其他用户自定义标示符:

andbreakdoelse
elseifendfalsefor
functionifinlocal
nilnotorrepeat
returnthentrueuntil
whilegoto

一般约定,以下划线开头连接一串大写字母的名字(比如 _VERSION)被保留用于 Lua 内部全局变量。

变量

赋值

name = 'yuankang'
io.write(name,' Size of string ', #name, '\n')
-- yuankang Size of string 8

name = 4
io.write("my name is ",name)
-- my name is 4

长字符串

longString = [[
I am a very very long
string that goes on
foever
]]

io.write(longString,"\n")

字符串拼接

longString = [[
I am a very very long
string that goes on
foever ]]

longString = longString .. name

io.write(longString)

空值

io.write(asdasfddsaf)
--我们并没有定义asdasfddsaf,所以此处打印的结果时nil

在lua中,任何没有定义的值都是nil

算数运算符

+
-
*
/
%取余

image-20200906191007994

这没啥好说的,值得注意的是,在取余的时候会舍去小数点

number = 1
-- 以下是不被允许的操作
number++
number--
number+=1
number-=1

数学运算方法

floor向下取整
ceil向上取整
sqrt平方
pow次方
log10对数
random随机
max最大值
min最小值
sin
cos
asin
acos
exp
log

image-20200906191957808

image-20200906192005368

逻辑运算符

>大于
<小于
>=大于等于
<=小于等于
==等于
~=不等于(不是!=)

条件判断

age = 13
if age < 16 then
    print("you can go to school")
    local localVar = 10
elseif (age <= 16) and (age < 18) then
    print("you can drive")
else
    print("you can vote")
end

print(localVar)
-- 这里因为用了local,所以不能跳出if语句块,此处是nil

if (age < 14) or (age > 67) then
    print("you shouldn't work\n")
end

print(string.format("not true = %s",tostring(not true)))
-- not true = false

三目运算符可以用吗?

-- canVote = age > 18 ? true : false        不被允许的写法
canVote = age > 18 and true or false 		-- 正确的写法

redis结合lua实现分布式锁安全删除

if redis.call('get',KEYS[1]) == ARGV[1] then
    return redis.call('del',KEYS[1])
    else return 0
end

-- 含义: 当get我们之前设置的lock,如果这个值是等于我们设置的lock的value,那么就马上进行删除,从而防止一些并发安全问题,比如get到了这个key,并且校验了这个lock_value是ok的,正准备去删除的时候,别的线程(节点)对这个lock进行了操作,把lock_value设置成了他的value,那么这个时候去执行删除,删除的是别人的lock,不安全了.

字符串操作

长度

astring = "asd as  asd fs a sdqwd sd s dasd asdas dfadsad asd af gsdasdas"
print("string length: ",#astring)	
print("string length: ",string.len(astring))
--string length: 	62
--string length: 	62

替换

quote = "I have a girlfriend"
print("real: ",string.gsub(quote,"have","don't have"),"\n")
--real: 	I don't have a girlfriend		

查找索引位置

print("Index of girlfriend: ",string.find(quote,"girlfriend"))
--Index of girlfriend: 	10	19

大小写转换

string.upper(quote)
string.lower(quote)

循环

while-do

i = 1
while(i <= 10) do
    print(i)
    i = i+1
    if i == 8 then break end
end

repeat-until

repeat
    io.write("Enter the key: ")
    guess = io.read()
until tonumber(guess) == 15

image-20200906202442041

for循环

-- 初始值,结束值,步长
for i = 1, 10, 2 do
    io.write(i)
end

--13579

table

更像是我们平常说的数组

像极了for range的样子

months = {"Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"}
for key,value in pairs(months) do
    io.write(key," ",value," ")
end

--1 Jan 2 Feb 3 Mar 4 Apr 5 May 6 June 7 July 8 Aug 9 Sep 10 Oct 11 Nov 12 Dec 

table插入元素

其实到这里可以看到,table相比于array其实更像是map

aTable = {}
for i = 1 ,10 ,1 do
    aTable[i] = i
end

for k,v in pairs(aTable) do
    io.write(" ",v)
end

table.insert(aTable,1,0)
print()
for k,v in pairs(aTable) do
    io.write(" ",v)
end

-- 1 2 3 4 5 6 7 8 9 10
-- 0 1 2 3 4 5 6 7 8 9 10

使用table.remove(tableName,index)可以将table中对应位置的元素删除

将table变成string

print(table.concat(aTable,","))
-- 0,1,2,3,4,5,6,7,8,9,10

二维

btable = {}
for i = 0,9 do
    btable[i] = {}
    for j = 0,9 do
        btable[i][j] = tostring(i) .. tostring(j)
    end
end

for i = 0 , 9 do
    for j = 0 ,9 do
        io.write(btable[i][j])
    end
    print()
end
--[[
00 01 02 03 04 05 06 07 08 09 
10 11 12 13 14 15 16 17 18 19 
20 21 22 23 24 25 26 27 28 29 
30 31 32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49 
50 51 52 53 54 55 56 57 58 59 
60 61 62 63 64 65 66 67 68 69 
70 71 72 73 74 75 76 77 78 79 
80 81 82 83 84 85 86 87 88 89 
90 91 92 93 94 95 96 97 98 99 
]]

function

function splitStr(theString)
    strtable = {}
    local i = 0
    for str in string.gmatch(theString,"[^%s]+") do
        strtable[i] = str
        i = i+1
    end
    return strtable,i
end

strTable,nums = splitStr("I like that"," ")
for i = 0,nums do
    io.write(string.format("%s",strTable[i]),",")
end

--I,like,that,

函数闭包

function outerFunc()
    local i = 0
    return function()
        i = i+1
        return i
    end
end

getI = outerFunc()
print(getI())
print(getI())

--[[
1
2
]]

协程

co = coroutine.create(function()
    for i = 1, 10, 1 do
        print(i)
        print(coroutine.status(co))
        if i == 5 then coroutine.yield() end
    end
end)

print(coroutine.status(co))
coroutine.resume(co)
print(coroutine.status(co))

co2 = coroutine.create(function()
    for i = 101,110 , 1 do
        print(i)
    end
end)
coroutine.resume(co)
coroutine.resume(co2)

print(coroutine.status(co))

文件操作

这部分后续再完善

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值