Lua学习

  1. 1、变量类型
  2. 2、string
  3. 3、Number
  4. 4、table
  5. 5、控制结构
  6. 6、函数
  7. 8、其他
  8. 测试代码:

1、变量类型

  • nil,Boolean,string,Number,table

2、string

​ 会根据上下文在合理的情况下进行数字和字符之间的转换。

print("8" + 8)  --> 16

print(“8 + 8”) –> 8 + 8
print(“hello world” + 8) –> ERROR (数字转string使用tostring(8))

:hexoPostRenderEscape–>

3、Number

  • 科学计数

    myNumber = 7.65e8 --> (7.65 x 10^8)
    

    myNumber = 7.65e-2 –> (7.65 x 10^-2)

    :hexoPostRenderEscape–>

  • 运算

  • 支持 +,-,*,/,比c语言多一个乘方运算“^”,2^3 = 8

    - 比较运算
    
      - “~=”是不等于
tostring(Number) --> 可以转换变量类型

str = (a and b)or c –> lua的三元运算a ? b :c
a,b = 10,2 –> 连续赋值 a = 10,b = 2
local x,y = 1,2 –x:1,y:2 – x,y局部变量声明
a,b = b,a –> 交换数值

:hexoPostRenderEscape–>

4、table

aa = {11,12,13,16}
print("aa[1]=",aa[1]) --> aa[1]=11,table默认没有aa[0]

w = {x=0,y=12,label=“console”} –> 键值对,可以使用下标访问成员w.x == 0,w.y == 12,w.label == “console”

z = {{x=1,y=2},{x1=11,y1=22},{x2=111,y2=222}} –> 不能使用下标访问
print(type(z[1].x),z[1].x) –>number 1
print(type(z[2].x1),z[2].x1) –>number 11
print(type(z[3].y2),z[3].y2) –>number 222

a = {[2] = “red”,[5] = “green”,[7] = “blue”} –> 重定义下标起始值
print(type(a[2]),a[2]) –>”red”
print(type(a[5]),a[5]) –>”green”
print(type(a[7]),a[7]) –>”blue”

revDays = {[“Sunday”] = 1, [“Monday”] = 2, –> 反向表
[“Tuesday”] = 3, [“Wednesday”] = 4,
[“Thursday”] = 5, [“Friday”] = 6,
[“Saturday”] = 7}
x = “Tuesday”
print(revDays[x]) –> 3

:hexoPostRenderEscape–>

5、控制结构

if/then需成对使用

  • if 条件 then 语句 else 语句 end

    a = 2
    if a>2 then
    print("a > 2")
    else
    print("a <= 2")
    end
    --> a > 2
  • if 条件 then 语句 elseif 条件 then 语句 else 语句 end

    a = 7
    if a > 7 then
      print("a > 7")
    elseif a == 7 then
      print("a = 7")
    else 
      print("a < 7")
    end
    --> a = 7
  • while 条件 do 语句 end

    a = 0
    while a<3 do
      print("a = ",a)
      a = a + 1
    end
    --> a = 0
    

    –> a = 1
    –> a = 2

    :hexoPostRenderEscape–>

  • for变量 = 初值,终点值,变量自加值 do 语句 end

    变量自加值不写默认是1

<!--hexoPostRenderEscape:<pre class="line-numbers language-lua" data-language="lua"><code class="language-lua">a <span class="token operator">=</span> <span class="token number">0</span>

for i=0,5,2 do
print(“a = “,a)
a = a + 1
end
–> a = 0
–> a = 1
–> a = 2

–> print all values of array ‘a’
for i,v in ipairs(a) do print(v) end:hexoPostRenderEscape–>

  • repeat 语句 until

    b = 0
    repeat
      b = b + 1
      print("b = ",b)    
    until b == 5

6、函数

  • function 函数名(…) 语句 end

    function aa(...) --> 形参是“...”时,Lua会创建一个局部的名字为arg的table
    

    print(arg.n) –> 传进来的参数个数
    print(arg[1])
    print(arg[2])
    print(arg[3])

    end
    aa(1,2,3,4,5,6) –>实参可以任意多

    :hexoPostRenderEscape–>

  • 返回函数

    函数参与拼接时只返回一个值

function a()
    return 1,2
end
function b()
    return a()
end
function c()
    return (a())
end
print("str"..a()) --> str1

print(a()) –> 1 2
print((a())) –> 1
print(b()) –> 1 2
print(c()) –> 1
print((b())) –> 1

function c()
return 2,3
end
y,u,i = c()10 –> c()后面有表达式,返回一个值,2,10,nil
y,u,i = 10c() –> c()后面无表达式,返回所有值,10,2,3

:hexoPostRenderEscape–>

8、其他

  • 拼接符

    不是纯数字的string之间不能直接使用“+”号进行拼接

ing = "ing"
print("str"..ing)  --> string

str = “str”..ing
print(str) –> string

str = “a12” + “a34”
print(str) –> error

str = “12” + “34”
print(type(str),str) –>number 46

:hexoPostRenderEscape–>

  • 注释
    “–”注释一行
    “–[[” “]]”注释一段

    • 作用域

      local a=1 --> 局部
      

      b=2 –> 全局
      c=3 –> 全局

      :hexoPostRenderEscape–>

    • 逻辑运算符

      只有false和nil才计算为false,其他的都是true,0也是true

a and b --> 如果a为false,则返回a,否则返回b

a or b –> 如果a为true,则返回a,否则返回b
print(not nil) –> true
print(not 0) –> false
print(not 1) –> false
str = (a and b)or c –> lua的三元运算a ? b :c

:hexoPostRenderEscape–>

  • 优先级

    从高到低的顺序
    ^
    not - (unary)
    * /
    + -
    ..
    < > <= >= ~= ==
    and
    or

测试代码:

代码 运行结果
a = 2.35
print(“Hello World!”,type(a),a)
a = nil
print(“Hello World!”,type(a),a)
a = “东东”
print(“Hello World!”,type(a),a)
a = true
print(“Hello World!”,type(a),a)
aa = {11,12,15,16,17}
print(“aa[0] = “,aa[0])
print(“aa[1] = “,aa[1])

a = 2
if a>2 then
print(“a > 2”)
else
print(“a <= 2”)
end

a = 7
if a > 7 then
print(“a > 7”)
elseif a == 7 then
print(“a = 7”)
else
print(“a < 7”)
end

a = 0
while a<3 do
print(“a = “,a)
a = a + 1
end

a = 0
for i=0,5,2 do
print(“a = “,a)
a = a + 1
end

b = 0
repeat
b = b + 1
print(“b = “,b)
until b == 5

ing = “ing”
print(“str”..ing)

str = “str”..ing
print(str)


–[[str = “string+” + 123
print(str)]]

print(“end”)
print(not nil)
print(not 0)
print(not 1)
Hello World! number 2.35
Hello World! nil nil
Hello World! string 东东
Hello World! boolean true
aa[0] = nil
aa[1] = 11
a <= 2
a = 7
a = 0
a = 1
a = 2
a = 0
a = 1
a = 2
b = 1
b = 2
b = 3
b = 4
b = 5
string
string
end
true
false
false

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2291184112@qq.com

×

喜欢就点赞,疼爱就打赏