Brief Introduction to Lua
Baidu just a moment (I will not write it myself if I am lazy)
Lua is a small scripting language. A research group at the Pontifical Catholic University of Rio de Janeiro,
It was developed in 1993 and is composed of Robert to ierusalimschy, Waldemar Celes, and Luiz Henrique De Figueiredo. It is designed to embed in applications,
This provides flexible scalability and customization for applications. Lua is compiled by standard C and can be compiled and run on almost all operating systems and platforms. Lua does not provide powerful libraries,
This is determined by its positioning. Therefore, Lua is not suitable for developing independent applications. Lua has a simultaneous JIT project that provides real-time compilation on a specific platform.
Naming Convention: Lua uses a naming convention similar to CSHARP which consists of any letter, number, and underline, but cannot begin with a number. In principle, do not use underscores to increase the number of variable names. Of course, you cannot use the Lua keyword, and Lua is case sensitive. Keyword:
| And |
Break |
Do |
Else |
Elseif |
End |
False |
| For |
Function |
If |
In |
Local |
Nil |
Not |
| Or |
Repeat |
Return |
Then |
True |
Until |
While |
Note: line comments use two hyphens (--). Multiple comments have two similarities, the equal signs ending with "--]" or ending with "-- [= [" Start with "-] =]" can be any number but must be matched before and after.
Global variables: The global variables in Lua are special and can be directly used without being declared. The nil value is not worth a long time. Delete the global variables directly to nil.
Data Type
Lua is a dynamic language with no type definition syntax. That is to say, a Lua variable can store both numeric values, string values, and other types. But we usually do not do this because it is easy to confuse. Lua defines 8Basic Type
| Type |
Name |
Type |
Name |
| Nil |
Null |
Boolean |
Boolean |
| Number |
Number |
String |
String |
| Userdata |
Custom type |
Function |
Function |
| Thread |
Thread |
Table |
Table |
Obtaining the variable type in Lua is also a simple and practical type function. Write a piece of code to test first:
Print (a) -- the value of A is nilprint (type (TMP) -- the value of TMP is nil, but it is not printed here, instead, the value type TMP = 3 -- assign an integer to TMP 3 Print (type (TMP) --> numbertmp = 3.14 -- assign a floating point print (type (TMP) to TMP )) --> numbertmp = print -- assign the function print to tmpprint (type (TMP) --> functiontmp = True Print (type (TMP )) --> booleantmp = Nil -- give the global variable nil a value equivalent to deleting print (type (TMP) --> Nil
The above code is very simple and there are comments that are easy to understand and will not be explained.
Boolean Type:Boolean is mainly used to judge truth and false in the branch structure. During the condition test, Lua only has two values that are false: false and nil. Any other value is considered to be true, including numbers and null strings.
Numeric type:The numeric types in Lua are quite concise, and there is no long integer in CSHARP, single-double-precision floating-point number, just a number real type.
String:The string in Lua is an unchangeable value, that is, it cannot be directly modified on the string. You can use double quotation marks or single quotation marks for a literal string, but we recommend that you use a uniform style. Escape characters are also supported in Lua.
| \ |
Ring tones |
\ B |
Return |
| \ F |
Provide tables |
\ N |
Line feed |
| \ R |
Enter |
\ T |
Horizontal Tab |
| \ V |
Vertical Tab |
\\ |
Backslash |
| \" |
Double quotation marks |
\' |
Single quotes |
In addition to escape characters, Lua also supports escaping numbers into characters based on ASCII code. In addition, you can use the brackets "[[" "]" to separate strings. However, the square brackets cannot handle all the situations. For example, the string contains a = B [C [I]. In this case, you can think of a block comment as follows, add any equal sign between square brackets.
String type conversion: When it comes to strings, you have to say the type conversion between strings and numbers. In Lua, The Lua interpreter automatically converts a string to a number or a number to a string based on the context. Let's take a look at the "type conversion" section of the following code:
A = "one string" B = 'one' print (a) --> one stringprint (B) --> oneb = string. gsub (a, "one", "other") -- execute string replacement. Of course, you can also write a = string. gsub (a, "one", "other") print (a) --> one stringprint (B) --> other string ------------------------------------- ASCII numeric escape ---------------- A = "Hello worl \ 100" Print (a) --> heloo world ------------------- block string, unescaped content ------------------- A = [<HTML>
Table type:The table in Lua implements the "Join array", which can index the Array Using any Lua type value other than nil. A more accurate understanding of a table should be an object. Use a pair of braces {} to create a table, and it is always anonymous. When no variable holds a reference to the table, Lua will automatically reclaim its memory. The following code shows the specific operations on the table:
----------------------- The table is created for basic access ------------------------ A ={}-- print the address table of a: 004fc330 (that is, reference) to create a table without any element print () TMP = "hjs" A [TMP] = 123 -- add a project (Key = hjs, value = 123 ), table dynamically adds a project (Key = 10, value = 20) with a global variable similar to a [10] = 20 -- add a project (Key = 10, value = 20) print (A [TMP]). --> 123 print (A [10]) --> 20b = A -- assign the table reference value to bprint (A [TMP]) --> 20 A [10] = 30 -- modify the print (A [10]) value of the element in the table --> 30 ------------- access to another table syntax -------------------------. name = "Xiaoyue" Print (. name) --> xiaoyueprint (A ["name"]) --> Xiaoyue description: these two statements are used to access the project -------------- with the key as the name in the table to obtain the table size ---------------------------- A = {} A [100] = 20 print (# A) --> 0 description: lua uses nil as the end mark of the array. The value of a [1] is nil. Here we get 0 (the subscript in Lua usually starts from 1) print (table. maxn (A) --> 100 use table. maxn functions can work well ------------- data type conversion of table indexes ----------------------- I = 10; j = "10 "; k = "+ 10" A = {} A [I] = "one" A [J] = "two" A [k] = "other" Print (A [I]) --> oneprint (A [J]) --> twoprint (A [k]) --> other description: all three I J K variables can be converted to the numeric type 10. However, data is not converted during table access, and all three keys are different from print (A [tonumber (j)]). --> oneprint (A [tonumber (k)]) --> one
Let's talk about the data type first. There are several functions and other types later.