B station daily porridge Lua study notes

variable declaration

In Lua, as long as assigning a variable is equal to declaring a variable

 

The declared variable is a global variable by default and can be called in other files. After adding local, it becomes a local variable and can only be called in the current function or code block.

 

The value of a variable that has not been declared is nil, and it is also regarded as a type. If it is not assigned, it is considered nil if it is not declared (similar to NULL in other languages)

 

 Multiple assignments are also possible in Lua, and the statement is also nil if no assignment is made.

 

 number type

All numeric types in Lua are number types, which cover all data types such as float and int, and also support scientific notation and hexadecimal notation

string type

Lua can use double quotes and single quotes to assign a string, and also supports shifting characters, such as /n, single and double quotes can also enter spaces, but not line breaks.

 Use two square brackets to represent multi-line text, which will include spaces and newlines. At the same time, escape characters such as /n will not be escaped.

 

 String links are represented by ..

 

 tostring can convert a number type to a character type, and tonumber can convert a character type to a number type. When the character type cannot be converted to a number type, its value is nil.

 

 The # sign can get the length of the string, and the escape character counts as one character.

 

 function

 The Lua function is written as follows, the two are equivalent, and the parameters are in parentheses. The return value of the function defaults to nil.

 Example of function usage

 Functions can also use return to return values, and can also perform multiple return values, and at the same time cooperate with multiple assignment statements

 

 table

 table is an important data structure in Lua. It is an associative array with unlimited size. It can store any index, number, string, table itself, function, etc., but it cannot store nil. Non-existent elements are nil.

 The default number subscript can be used in the table, and the number subscript starts from 1 instead of zero. Values ​​can also be assigned in the table to add elements to the table. You can also use the # sign to get the table length.

 

 

 

 You can also use the built-in table interface to perform insertion operations. You can insert a value at a specified position, and the original element will move down sequentially. If there is no specified position, it will be inserted at the end by default.

 

 There is also a remove operation, which can also be returned while removing.

 

 Strings can also be used as subscripts in the table, and double quotes must be added to the subscripts when calling.

 

 If it conforms to the variable naming convention, it can be represented by a . sign.

 

 If it does not meet, there are the following expressions

 

You can also directly assign values ​​to subscripts. (But it should not be directly added after the table, at this time #a outputs the table length as 1)

 

 global table

Lua has a special table global table _G, all global variables are in the global table _G, and can be read and printed in _G.

 table is also a global variable, which can be printed in _G, and insert is a function in table, which can also be printed in _G.

 

 

 calculating signs

 Lua is basically the same as others, but it does not mean that it is represented by ~=

In Lua, except false and nil represent false, others are true. In c, the number 0 is also false, but in Lua, the number 0 is true .

In Lua, if the variable is a value instead of true, the value will be returned directly

and returns the latter value if both sides are true.

 

 

 The if judgment statement in Lua is as follows

 

The for loop in Lua is as follows,

 

 A step size can also be added after the for loop, which can be a negative number to achieve flashback. There is no self-increment and self-decrement operation in Lua.

 

 Note that the value modification of i in the for loop is invalid, and i cannot be operated on in the execution statement. If the operation is performed on i, it will be converted into a new local form i, which has no effect on i in the loop body. But a break can be performed in a for loop

 

 while loop

Similar to other languages, you can also break

 

repeat cycle

Similar to while, but written differently.

 

 

 string type

 Similar to string numbers in c. char can transfer ascii code.

 

byte can take out characters at any position, and the same subscript starts from 1 instead of 0

 

Of course, 0x00 will not end the string, it will be directly stored as 0, and the string 0 in c will end the string.

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_53211468/article/details/125127186