美文网首页
Lua 语法入门

Lua 语法入门

作者: awker | 来源:发表于2018-12-18 01:46 被阅读0次

赋值

lua 赋值支持多个值同时赋值
a = 1
b = "foo"
print(a, b)
$ ./lua53.exe hello.lua
1       foo

a, b = 1, 2
print(a, b)
$ ./lua53.exe hello.lua
1       2

a, b = 1
print(a, b)
$ ./lua53.exe hello.lua
1       nil

a, b= 1, 2, 3
print(a, b)
$ ./lua53.exe hello.lua
1       2

a, b = 1, 2
a, b = b, a
print(a, b)
$ ./lua53.exe hello.lua
2       1

table

lua table 的 key 可以是数字也可以是字符串,并且 key 的索引是从 1 开始的
  1. table 的数字key
t1 = {}
t1[1] = 10
t1[2] = "foo"
print(t1[1],t1[2],t1[3])

t2 = {
    20,
    "bar"
}
print(t2[1], t2[2])

$ ./lua53.exe hello.lua
10      foo     nil
20      bar
  1. table 的字符串key
t3 = {}
t3["name"] = "foo"
print(t3.name)
$ ./lua53.exe hello.lua
foo

t4 = {
    ["name"] = "bar",
    ["age"] = 20,
    [3] = false,
}
print(t4.name, t4.age, t4[3])

$ ./lua53.exe hello.lua
bar     20      false

函数

lua 函数用 function 做关键字,用 end 作为结束符。函数可以返回多个值,也可以赋值给变量
function add( a, b )
    return a + b
end

result = add(1 ,"2")
print(add(1, 2), result)
$ ./lua53.exe hello.lua
3       3.0

sub = function ( x, y )
    return x - y 
end
print(sub(2, 1))
$ ./lua53.exe hello.lua
1

function add_sub( x, y )
    return x + y, x - y
end
print(add_sub(1, 2))
$ ./lua53.exe hello.lua
3       -1

表达式

字符串用 .. 连接,用 local 声明本地变量
print(1 + 2)
print(true and false)
print(true or false)
print(not false)
print("foo" .. "bar")
$ ./lua53.exe hello.lua
3
false
true
true
foobar

function foo( ... )
    x = 1
    local y = 2    -- 只存在此函数作用域里
end
foo()
print(x)
print(y)
$ ./lua53.exe hello.lua
1
nil

条件语句

if elseif else end
x, y, z = 10, 20, 30
if x > 10 then
    print(x)
elseif y > 20 then
    print(y)
else
    print(x, y ,z)
end
$ ./lua53.exe hello.lua
10      20      30

循环语句

while do end; for do end; for in do end;
local i = 0
while i < 3 do
    print(i)
    i = i + 1
end
$ ./lua53.exe hello.lua
0
1
2

for i = 0, 3 do
    print(i)
end
print('-----')
for i = 0, 3, 2 do
    print(i)
end
print('*****')
for i = 3, 0, -1 do
    print(i)
end
$ ./lua53.exe hello.lua
0
1
2
3
-----
0
2
*****
3
2
1
0

t = {
    ['name'] = 'foo',
    ['age'] = 10
}
for k, v in pairs(t) do
    print(k, v)
end
$ ./lua53.exe hello.lua
name    foo
age     10

kv = {
    ['name'] = 'foo',
    ['age'] = 10,
    
    [1] = 10,
    [2] = 30,
}

for k, v in pairs(kv) do 
    print(k, v)
end
print('-----')
for k, v in ipairs(kv) do
    print(k, v)
end

$ ./lua53.exe hello.lua
2       30
age     10
name    foo
1       10
-----
1       10
2       30

require
// foo.lua
local class = {}

-- class.foo = function ( a, b )
--  return a + b
-- end

function class.foo ( a, b )
    return a + b
end

return class

// hello.lua
local c = require("foo")
print(c.foo(1, 2))

$ ./lua53.exe hello.lua
3

系统库

table.insert table.remove # type tonumber tostring string.format
local t = {}
for i = 1, 10 do
    table.insert(t, i)
end

table.remove(t, 2)

for k, v in pairs(t) do
    print(k, v)
end

$ ./lua53.exe hello.lua
1       1
2       3
3       4
4       5
5       6
6       7
7       8
8       9
9       10


local t = {}

t.a = 1
t.b = 2

t.a = nil

for k, v in pairs(t) do
    print(k, v)
end
$ ./lua53.exe hello.lua
b       2

local t = {5, "1", 3, 4}
local x = "hello world"
local y = {1, 2}
print(#t, #x, #y, type(y))
print(tostring(t[1]), type(tostring(t[1])), tonumber(t[2]), type(tonumber(t[2])))
print(string.format("hello %d", y[2]))
$ ./lua53.exe hello.lua
4       11      2       table
5       string  1       number
hello 2

相关文章

  • Lua 语法入门

    赋值 lua 赋值支持多个值同时赋值 table lua table 的 key 可以是数字也可以是字符串,并且 ...

  • LUA_API lua_absindex

    本系列不会讲 Lua 的基础语法,由于Lua的轻便简洁,读者自行搜索了解,很快就可以入门。本节开始,将直接进入 L...

  • 【IOS开发高级系列】Lua与OC交互专题

    1 Lua语法 Lua教程 http://www.yiibai.com/lua/lua_environment.h...

  • Lua

    让Xcode 支持 Lua 语法高亮 1.让Xcode支援Lua语法高亮(Syntax Highlighting)...

  • Lua 完全教程

    Lua 环境安装 Lua 基本语法 Lua 数据类型 Lua 数据类型:nil(空) Lua 数据类型:boole...

  • Lua学习路线

    1、lua语法:菜鸟教程 http://www.runoob.com/lua/lua-basic-syntax.h...

  • lua开发文章集锦

    lua入门教程:http://www.runoob.com/lua/lua-tutorial.html nginx...

  • Lua 快速笔记(二) syntax

    参考programing in lua 语法 函数 常用函数 语法 局部变量和代码块 控制语句 Lua 认为 fa...

  • [code.openresty] Openresty指令集-下

    log_by_lua 语法: log_by_lua 上下文: http,serv...

  • lua入门笔记 目录

    lua的中文API lua入门笔记1 类型 表达式 语句 函数lua入门笔记2 深入函数 深入函数 迭代器与泛型f...

网友评论

      本文标题:Lua 语法入门

      本文链接:https://www.haomeiwen.com/subject/hxzhkqtx.html