Lua basic knowledge learning one

Lua basic knowledge learning one

Learn the basics of Lua

1. Output "HelloWorld"

When using print("HelloWorld"),
the general Lua language does not need to add the ; sign at the end.

2. Identifier

1. Use all uppercase letters and underscores for constants
2. The first letter of a variable is lowercase
3. The first letter of a global variable is represented by a lowercase g
4. The first letter of a function (method) is uppercase

3. Variables

Variables do not need to be typed (weakly typed language)
and the type of the variable can be changed at any time

3.1 Global variables

Although variables can be defined at will, it is not recommended to define variables whose usage is unknown.

3.2 Local variables

After using the local keyword before a variable, the variable will become a local variable.

4.Types of Lua

4.1 Basic types

1. Empty nil type can destroy the storage space of variables. If you want to delete a global variable, assign the value of nil to the global variable.

2. boolean Boolean type

3. string string type

4. There is no integer or decimal in the number type Lua

5. table type represents a set subscript number starting from 1.
Example:
tab={parameter 1, parameter 2, parameter 3}

The initial subscript value of tab is 1, not 0

If you print(tab) directly
, the address value of table type is displayed.

6. Function represents the function type (method) in Lua

7. thread independent thread type (pseudo multi-threading)

4.2 Determine variable type

The method type(variable)
can get the type of the variable

5. String operations

5.1 String concatenation

Use "String 1". ."String 2" to connect strings without spaces in the middle.

5.2 Get the length of the string

1. Use the method string.len("string")
2. Use the # sign + string variable concatenation.
For example,
#"asgeher", string.len("aosbiie"),
the result is 7

5.3 Convert all strings to uppercase

string.upper(parameter 1)
parameter 1: string

5.4 Convert all strings to lowercase

string.lower (parameter 1)
parameter 1: string

5.5 Intercept the specified string length

string.sub(parameter 1, parameter 2, parameter 3)
parameter 1: string:
parameter 2: interception starting point
parameter 3: interception length

5.6 Reverse string

string.reverse (Parameter 1)
Parameter 1: String
Function: Reverse all the values ​​​​of the string.
For example:
String: "abcdefg"
After the reverse, the string is: "gfedcba"

5.7 Formatting of strings

string.format (Parameter 1)
Parameter 1: String
Pass the parameter into the string and change the value of the string.
For example,
name="xxx"
age=20
print(string.format("My name is %s, my year is %d years old", name, age))
is displayed as: My name is xxx, I am 20 years old this year

%d is represented as a numeric variable
%s is represented as a string variable

5.8 Searching for strings

5.8.1 Format 1

string.find (parameter 1, parameter 2)
Parameter 1: string
Parameter 2: Specify a string that exists in the string, and return the subscript position of the string after searching.
For example:
num= string.find("aoaib ","i")
the value of num is 4

5.8.2 Format 2

string.find (parameter 1, parameter 2, parameter 3)
Parameter 1: String
Parameter 2: Specify a string that exists in the string. After searching, return the subscript position of the string.
Parameter 3: Specify which string to download from. Start searching for the value with the subscript number
. For example:
num= string.find("aoaibsababiao", "i",10) If
the value of num is 11
, it will automatically skip the first i character and start searching from the subscript 10.

5.9 String replacement

string.gsub(parameter 1, parameter 2, parameter 3)
parameter 1: original string
parameter 2: a certain string in the original string
parameter 3: the value to be replaced

6. Relational operations

6.1 Symbols

Less than sign: <
Less than or equal sign: <=
Greater than sign: >
Greater than or equal sign: >=
Equal sign: ==
Not equal sign: ~=

7. Logical operators

7.1 Symbols

or: or
and: and
non: not

7.2 Usage

7.2.1 or usage (or)

If two relational expressions are used to judge, use or to connect the two expressions.
As long as one relational expression is true, the entire expression is true.

7.2.2 and Usage (and)

If two relational expressions are used for judgment, and is used to connect the two expressions
. The value of both relational expressions must be true for the value of the entire expression to be true.

7.2.3 not usage (not)

Commonly used to judge boolean type values. Use not
in front of the relational expression to connect. If the Boolean value is true, it becomes false. If it is false, it becomes true.

Example
isbool=true
print(not(isbool))

*8. Multiple assignment

8.1 Definition

The number of variables is consistent with the number of assigned contents. If the assigned contents are more than variables, then it will be nil.
For example:
a1, a2, a3, a4, a5 = 100, 200, 300.
In the above example,
the values ​​of a4 and a5 are nil.

9. Judgment expression

9.1 if statement

Format

if condition then
- - body
end

condition : relational expression, the content to be judged
body : the content to be executed
end : the end mark of the judgment statement

9.2 else if statement

if condition then
- - body
elseif condition then
- - body
end

Same semantics as above! ! !

10. Comments

10.1 Single-line comments - -
10.2 Multi-line comments- -[[]]- -

11.for loop

11.1 Structure

1. for i = parameter 1, parameter 2, parameter 3 dobody end


Parameter 1: Starting value
Parameter 2: Maximum value
Parameter 3: Step size (the value required for one loop)
*do: Acts as the starting value of curly braces in C# end
: The end mark of the for loop
2. for Parameter 1 ,
Parameter 2 in ipairs (parameter 3) do
- -body
end
Parameter 1: key value
Parameter 2: table value
Parameter 3: table, equivalent to a collection, array
*do: acts as the starting value of curly braces in C#
end: the end of the for loop mark

11.2 Breaking out of the loop

11.2.1 break

Meet the conditions and jump out of the entire loop

11.2.2 return

Meet the conditions and jump out of the entire loop

There is no continue. The Lua language does not use continue as the keyword to jump out of the loop, and this keyword does not

12. repeat loop (while loop)

12. 1 repeat

repeat
- -body
until judgment expression
judgment expression: the conditions of the judgment expression must be met to end the loop
body: the content to be executed

while judgment expression do
- -body
end
judgment expression: Just like the ordinary for loop, as long as it is limited to a certain range, it can be looped
body: the content to be executed

13. Function

A method must be defined using the function keyword to be a method

Guess you like

Origin blog.csdn.net/qq_46005796/article/details/115627718