lua

[[toc]]

Getting started with lua.

Lua 5.4 Reference Manual

comment

The line comments and block comments.

1
2
3
4
5
6
a = 1
print(a = 1)
--Line comments
--[[
block comments
]]

Variables

It is okay to have a semicoion at the end.

the declaration variable defaults to global.

1
2
3
a = 1;
b = 2
print(a)

Output:

1

local variable

1
local a = 1

Variables that have not been declared in lua are nil, Similar to nullptr in C++.

1
print(c)

Output

nil

Multiple assignment

The default value without assignment is nil

1
2
a, b, c = 1, 2
print(a, b, c)

Output:

1 2 nil

Number

In the C language,there are multiple types of number values,such as int,char,long,etc.,but in lua they are collectively called number.

1
2
3
4
a = 1
b = 2e10
c = 0x11
--Hexadecimal

Operator

1
2
3
a = 1
b = 2
print(a + b)

Output:

3

string

1
2
3
4
5
6
a = "I am string type\n"
b = 'I am also a string type\n'
c = [[
The contents of the remain unchanged.\n
]]
print(a, b, c)

Output:

I am string type
I am alse a string type
The contents of the remain unchanged.

We found that the content in [[]] remained the same,and the transliteration characters did not change.

In lua the string concatenation is not a +,but ...

1
2
3
4
a = 'the '
b = 'lua'
c = a..b
print(c)

Output:

the lua

String and numbers can alse be converted to each other. the return value of tonumber() conversion failure is nil.

1
2
3
a = tostring(10)
b = tonumber('10')
c = tonumber("abc")

Output:

10 10 nil

Get the string length by #

1
2
a = "hello world"
print(#a)

Output:

11

Function

declared function

The following function declares an equivalence, ... is the function argument.

1
2
3
4
5
6
7
8
9
10
11
function function_name1(...)
-- body
end

function_name2 = function(...)
-- body
end

function function_name3(...)
-- body
end

such as:

1
2
3
4
5
function f(a, b)
print(a, b)
end

f(1, 2)

Output:

1 2

The default return value of the function is alse nil.

1
2
3
4
5
function f(a, b)
return a, b
end

print(f(1, 2))

Output:

1 2

table

digital subscript:

Can store numbers or strings,and even other tables,function,That is to say,anything can be stored in it.

It should be noted that the first subscript of the table is 1, you can also get the length of the table by #.

1
2
3
a = {1, "abc", {}, function() end}
print(a[1])
print(#a)

Output:

1 4

table.insert()

1
2
3
4
5
a = {1, "ac", {}, function() end}
a[5] = 123
table.insert(a, "d")
-- insert at the end
print(#a)

Output:

6

Insert a value at the specified position and move the following elements backward.

1
2
3
4
a = {1, "ac", {}, function() end}
a[5] = 123
table.insert(a, 2, "d")
print(a[2])

Output:

d

table.remove()

remove an element from an array

1
2
3
4
5
6
a = {1, "ac", {}, function() end}
a[5] = 123
table.insert(a, 2, "d")
local s = table.remove(a, 2)
print(a[2])
print(s)

Output:

ac d

string subscript:

It should be noted that each end must be followed by ,

1
2
3
4
5
6
7
8
9
10
11
a = {
a = 1,
b = "1234",
c = function()

end,
d = 123
}

print(a["a"])
print(a.a)

Output:

1 1

_G

global table

1
print(_G)

Output:

table: 0x3

As long as the declared global variables will appear in _G.

1
2
a = 1
print(_G["a"])

Output:

1

boolean

1
2
3
4
a = true
b = false
print(1 ~= 2)
-- not equal to(1 != 2)

and, or, not

1
2
3
4
5
a = true
b = false
print(a and b)
print(a or b)
print(not a)

Output:

false true false

In lua,only false and nil represent false,all other are true

1
2
3
4
5
a = nil --false
b = 0 -- true
print(a and b)
print(a or b)
print(not a)

Output:

nil 0 true

1
2
a = 5
print(a > 10 and "yes" or "no")

Output:

no

similar to c++

1
2
3
4
5
int a = 5;
if(a > 10)
cout << "yes";
else
cout << "no"

branch statement

1
2
3
4
5
6
7
if 1>10 then
print("1>10")
elseif 1 < 10 then
print("1 < 10")
else
print("no")
end

Output:

1 < 10

0 is the true

1
2
3
if 0 then
print("0 is true!")
end

Output:

0 is true!

loop statement

for

1
2
3
for i = 1, 10 do
print(i)
end

Output:

1 2 3 4 5 6 7 8 9 10

1
2
3
for i = 1, 10, 2 do
print(i)
end

Output:

1 3 5 7 9

2 is the step size.

The for loop variable can not be modified in the middle.

1
2
3
4
for i = 10, 1, -1 do
print(i)
if i == 5 then break end
end

Output:

10 9 8 7 6 5

while

1
2
3
4
5
local n = 10
while n > 1 do
print(n)
n = n - 1
end

Output:

10 9 8 7 6 5 4 3 2

Decrement is not supported in lua, such as

1
2
3
local n = 10
n--
n -= 1

string

1
2
3
4
s = string.char(0x30,0x31,0x32,0x33) --ascii
n = string.byte(s, 2)
print(s)
print(n)

Output:

0123 49