lua
[[toc]]
Getting started with lua.
comment
The line comments and block comments.
1 | a = 1 |
Variables
It is okay to have a semicoion at the end.
the declaration variable defaults to global.
1 | a = 1; |
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 | a, b, c = 1, 2 |
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 | a = 1 |
Operator
1 | a = 1 |
Output:
3
string
1 | a = "I am string type\n" |
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 | a = 'the ' |
Output:
the lua
String and numbers can alse be converted to each other. the return
value of tonumber()
conversion failure is
nil
.
1 | a = tostring(10) |
Output:
10 10 nil
Get the string length by #
1 | a = "hello world" |
Output:
11
Function
declared function
The following function declares an equivalence, ...
is
the function argument.
1 | function function_name1(...) |
such as:
1 | function f(a, b) |
Output:
1 2
The default return value of the function is alse
nil
.
1 | function f(a, b) |
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 | a = {1, "abc", {}, function() end} |
Output:
1 4
table.insert()
1 | a = {1, "ac", {}, function() end} |
Output:
6
Insert a value at the specified position and move the following elements backward.
1 | a = {1, "ac", {}, function() end} |
Output:
d
table.remove()
remove an element from an array
1 | a = {1, "ac", {}, function() end} |
Output:
ac d
string subscript:
It should be noted that each end must be followed by
,
1 | 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 | a = 1 |
Output:
1
boolean
1 | a = true |
and, or, not
1 | a = true |
Output:
false true false
In lua,only false
and nil
represent
false
,all other are true
1 | a = nil --false |
Output:
nil 0 true
1 | a = 5 |
Output:
no
similar to c++
1 | int a = 5; |
branch statement
1 | if 1>10 then |
Output:
1 < 10
0 is the true
1 | if 0 then |
Output:
0 is true!
loop statement
for
1 | for i = 1, 10 do |
Output:
1 2 3 4 5 6 7 8 9 10
1 | for i = 1, 10, 2 do |
Output:
1 3 5 7 9
2 is the step size.
The for loop variable can not be modified in the middle.
1 | for i = 10, 1, -1 do |
Output:
10 9 8 7 6 5
while
1 | local n = 10 |
Output:
10 9 8 7 6 5 4 3 2
Decrement is not supported in lua, such as
1 | local n = 10 |
string
1 | s = string.char(0x30,0x31,0x32,0x33) --ascii |
Output:
0123 49