The types and values of Lua learning notes _lua

Source: Internet
Author: User
Tags lua

Basic introduction

Lua is a dynamically typed language. There is no type-defined syntax in the language, and each value has its own type information. There are 8 basic types in Lua, respectively:

Nil (empty) type

Boolean (Boolean) type

Number (numeric) type

String (String) type

UserData (custom type)

function (function) type

Thread (threads) type

Table (table) type

These are the basic types of 8 in Lua, and we can use the type function to judge a worthy type, and the type function returns a string description of the corresponding type. For example:

Local Ivalue =
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 (empty)

Nil is a type that has only one value nil. The default value of a global variable before the first assignment is nil, and assigning nil to a global variable is equivalent to deleting it. Lua uses nil to represent an "invalid value" situation, that is, there is no valid merit.

Boolean (Boolean)

The Boolean type has two optional values: false and True. It is important to note that only false and nil are "false" in Lua, and the other is "true", which differs from other languages. I had a colleague before and I had this loss.

Number (digit)

The number type is used to represent double-precision floating-point numbers. LUA does not have an integer type, and the number in Lua can represent any 32-bit integer.

String (strings)

Strings in Lua typically represent "one character sequence". LUA fully employs 8-bit coding. The LUA string is a immutable value. Instead of directly modifying a character of a string as in C, you should create a new string based on the modification requirements. LUA strings and other objects are objects that are managed by the automatic memory management mechanism and do not need to worry about memory allocation and deallocation of strings. In Lua, strings can efficiently handle long strings. When a string is multiline, you can use the [[]] symbol to define a multiline string, and Lua does not interpret the escape sequence. For example:

Local page = [[
 
 

Table (Tables)

The table type implements an associative array, which is an array of special indexes, which can be indexed not only by integers, but also by strings or other types of values (except nil). In addition, the table has no fixed size and can be dynamically added to a table with any number of elements.

In Lua, a table is neither a "value" nor a "variable", but an object. You can think of a table as a dynamically assigned object, with only one team in the program (pointers). The creation of the table is done through the construction expression, and the simplest construction expression is {}.

The table is always anonymous, and there is no fixed correlation between a variable referencing the table and the table itself, such as the following code:

Local A = {}--creates a table and stores its reference in a
a["x"] = ten local
B = A-B and a refer to the same table
print (b["x"])
b["x"] = 20
   print (a["x"])

B = nil--now only a is referencing table-
-Error: Print (b["x"])
print (a["x"])
a = nil--There is no reference to table now

When a reference to a table is 0 o'clock, the LUA garbage collector eventually deletes the table and frees up the memory space it occupies. When an element of a table is not initialized, its contents are nil, and as a global variable, nil is assigned to an element of the table to remove the element.

In Lua, the form of a["name" is a simpler way of writing and can be entered directly into A.name. First look at the following code:

Local A = {}--creates a table and stores its reference in a
a["x"] = ten local
b = A--B and a refer to the same table
print (b["x"])
b["x" = 2 0
Print (a["x"])

B = nil--now only a is referencing table-
-Error: Print (b["x"])
print (a["x"])
a = nil- There is no reference to a table now

This type of writing itself provides simplicity, but sometimes it brings confusion to programmers, and I often make mistakes with a.x and a[x, a.x means a["x" to index a table with the string "X", and a[x is index table with the value of the variable x. Use the following code to see the difference between them:

Local A = {}
a["name" = Ten
print (a.name)--equivalent to print (a["name")

In Lua 5.1, the length operator "#" is used to return the last index value of an array or a linear table. In actual projects, we often use this operator to get the length of an array or a linear table. But using this operator is a trap, like the following code:

Local A = {}
x= "y"
a[x] = ten
print (a[x])-->10 equivalent a["y"
print (a.x)  -->nil equivalent to a["x"]
Print (A.Y)  -->10 equivalent to a["Y"]

How much does this output?

In Lua, the index results for all uninitialized elements are nil. Lua takes nil as a flag to define the end of an array. When an array has "voids", that is, the middle contains nil, the length operator considers these nil elements to be the end tag. Because a[1] = nil, the output for the above code should be 0. So, when dealing with table, you need to consider this problem. So how do you get the length of a table that contains nil? We can use TABLE.MAXN, which returns the maximum number of positive indexes for a table, as follows:

Local A = {}
a[1000] = 1
print (TABLE.MAXN (a))-->1000

function (Functions)

In Lua, a function is treated as a value, which means that a function can be stored in a variable, passed to other functions by argument, and can be the return value of other functions. LUA can call functions written in its own Lua language, and can invoke functions written in C language. All of the standard libraries in Lua are written in C language. I'll also go into a detailed summary of the functions in Lua later. So much to say here.

UserData (custom type) and thread (thread)

UserData is used to represent a new type created by an application or a C language library. Because the UserData type can store any of the C language data in a LUA variable. In Lua, this type does not have too many predefined operations and can only be assigned and tested for equality.

Thread is mainly used for "collaborative programs."

Summarize

This is basically the basic type of LUA literacy article, I hope to be a bit of use.

The above is the entire contents of this article, I hope to be able to learn the Lua language help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.