Basic introduction
Lua is a dynamically typed language. There is no type-defined syntax in the language, and each value carries its own type information. There are 8 basic types in Lua, namely:
nil (empty) type
boolean type
Number type
string type
userdata (custom type)
function type
thread type
table type
The above are the basic types in Lua 8. We can use the type function to judge a worthy type. The type function returns a string description of the corresponding type. For example:
local iValue = 10 local fValue = 10.2 local strValue = "Hello World" local funcValue = print local bValue = true local nilValue = nil local tbValue = {} if type(iValue) == "number" then print("It is a number") end if type(fValue) == "number" then print("It is a number") end if type(strValue) == "string" then print("It is a string") end if type(funcValue) == "function" then print("It is a function") end if type(bValue) == "boolean" then print("It is a boolean") end if type(nilValue) == "nil" then print("It is a nil") end if type(tbValue) == "table" then print("It is a table") end nil(null)
nil is a type that has only one value nil. The default value of a global variable before the first assignment is nil, assigning nil to a global variable is equivalent to deleting it. Lua uses nil to represent a "invalid value", i.e. there is no valid value situation.
boolean
The boolean type has two optional values: false and true. It must be noted that in Lua, only false and nil are "false", while other than that, they are "true", which is different from other languages. I had a colleague before, who suffered this loss.
number (number)
The number type is used to represent double-precision floating point numbers. Lua has no integer type, and numbers in Lua can represent any 32-bit integer.
string (string)
A string in Lua usually means "a sequence of characters". Lua is fully 8-bit encoding. Lua's string is an immutable value. You cannot directly modify a character of a string like in C language, but you should create a new string according to the modification requirements. Lua's strings and other objects are objects managed by the automatic memory management mechanism, so there is no need to worry about the memory allocation and release of strings. In Lua, strings can efficiently process long strings. When a string exists with multiple lines, you can use the "[[]]" symbol to define a multi-line string, and Lua will not interpret the escape sequence in it. For example:
local page = [[ <html xmlns="http:///1999/xhtml"> <head> <title>xxxx</title> </head> <body> </body> </html> ]] print(page)
table (table)
The table type implements an associative array, which is an array with a special indexing method; it can not only be indexed by integers, but also using strings or other types of values (except nil) to index it. In addition, the table has no fixed size, and any number of elements can be added dynamically to a table.
In Lua, a table is neither a "value" nor a "variable", but an object. You can think of a table as a dynamically allocated object, with only one team of their references (pointers) in the program. The creation of a table is done through "construction expression", and the simplest constructed expression is {}.
Table is always anonymous, and there is no fixed correlation between a variable that refers to the table and the table itself, such as the following code:
local a = {} -- Create atable,and store its reference ina a["x"] = 10 local b = a -- bandaQuoting the sametable print(b["x"]) b["x"] = 20 print(a["x"]) b = nil -- Now onlyaStill citingtable -- mistake:print(b["x"]) print(a["x"]) a = nil -- Nothing exists nowtableQuotation
When a reference to a table is 0, Lua's garbage collector will eventually delete the table and free up the memory space it takes. When an element of the table is not initialized, its content is nil; in addition, it can also be assigned nil to an element of the table like a global variable to delete the element.
In Lua, a simpler way to write a["name"] is provided, and can be entered directly. Let's take a look at the following code:
local a = {} -- Create atable,and store its reference ina a["x"] = 10 local b = a -- bandaQuoting the sametable print(b["x"]) b["x"] = 20 print(a["x"]) b = nil -- Now onlyaStill citingtable -- mistake:print(b["x"]) print(a["x"]) a = nil -- Nothing exists nowtableQuotation
This writing method itself provides simplicity, but sometimes it brings confusion to programmers; I often make mistakes with a[x], indicating a["x"], indicating indexing table with string "x"; and a[x] indexing table with the value of variable x. Let's take a look at the difference between them with the following code:
local a = {} a["name"] = 10 print() -- Equivalent toprint(a["name"])
In Lua 5.1, the length operator "#" is used to return the last index value of an array or linear table. In actual projects, we often use this operator to get the length of an array or linear table. However, there are traps when using this operator, such as the following code:
local a = {} x= "y" a[x] = 10 print(a[x]) -->10 Equivalent toa["y"] print() -->nil Equivalent toa["x"] print() -->10 Equivalent toa["y"]
How much should this output?
In Lua, the index result for all uninitialized elements is nil. Lua uses nil as a flag that defines the end of an array. When an array has "space", that is, when nil is contained in the middle, the length operator will consider these nil elements to be the end mark. Because a[1] = nil, the output for the above code should be 0. Therefore, when dealing with tables, this issue needs to be considered. So for a table containing nil, how to get its length? We can use it, which will return the maximum positive index of a table, as shown below:
local a = {} a[1000] = 1 print((a)) -->1000
function(function)
In Lua, functions are treated as values, which means that functions can be stored in variables, can be passed to other functions through parameters, and can also be used as return values for other functions. Lua can not only call functions written in Lua language, but also calls functions written in C language. All Lua's standard libraries are written in C language. I will summarize the functions in Lua in detail later. That's all I have to say here.
userdata (custom type) and thread (thread)
userdata is used to represent a new type created by an application or C library. Because the userdata type can store any C language data into Lua variables. In Lua, this type does not have many predefined operations, and can only perform assignment and equality testing.
thread is mainly used for "corordinated programs".
Summarize
This article is basically a basic type of literacy article in Lua, and I hope it is useful to everyone.
The above is all about this article. I hope it will be helpful for everyone to learn Lua language.