Types and values in Lua

Source: Internet
Author: User
Tags lua

Lua is a dynamic type of language that has no type-defined syntax in the language, and each value "carries" its own type information.

There are eight basic types in Lua, namely:

1.nil (empty)

2.boolean (BOOL)

3.number (digital)

4.string (String)

5.userdata (custom type)

6.function (function)

7.thread (thread)

8.table (table)

We can use the type function to determine a worthwhile type, and the type function returns a string description of the corresponding type.

Now let's take a look at what these eight kinds of meanings are


Nil (empty)

Nil is a type, it has only one value, which is nil, and its main function is to distinguish any other value. The default value for a global variable before the first assignment is nil, and nil is assigned to a global variable equivalent to deleting it.


Boolean (Boolean)

The Boolean type has two optional values: false and True,lua treat the value false and nil as false, and the other values as true. We can use this approach when we are making conditional judgments:

--condition for the judging condition if not condition then--if it is not trueelse--if it is TrueEnd


number (numeric)

The number type is used to represent real numbers. Can be used to represent any 32-bit integer without generating a rounding error.


String (String)

strings in Lua typically represent "a sequence of characters". LUA fully uses 8-bit encoding. LUA's strings are immutable values. Instead of modifying a character of a string directly as in C, you should create a new string based on the modification requirement. LUA's strings and other objects are objects managed by the automatic memory management mechanism and do not need to worry about the memory allocation and deallocation of strings. In Lua, strings can handle long strings efficiently. When a string is multiple rows, you can use the "[[]]" notation to define a multiline string, and Lua does not interpret the escape sequence.

Please note that the red bold font, some friends may mistakenly think that the whole string can not be modified, see the following example:

Local S1 = "fat" local s2 = S1S1 = "Monkey" local s3 = "man" Local S4 = String.gsub (S3, "Han", "Sister") print (S1. "_" .. S2) Print (S3. "_" .. S4)

The output is:

Monkey _ Fat Man _ sister


Table (Tables)

The table type implements an associative array, which is an array with a special index, which can be indexed not only by integers, but also by strings or other types of values (except nil). In addition, table does not have a fixed size, you can dynamically add any number of elements to a table. in Lua, 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 table is done through the construction expression, and the simplest constructed expression is {}. table is always anonymous, there is no fixed correlation between a variable referencing table and the table itself, such as the following code:        

Local T1 = {}--Create a table and store its references in T1 t1["a"] = "HI"-new entry, key= "a", value= "HI" local t2 = T1--t1 and T2 refer to the same tableprint (t2["a" ]) t2["a"] = "Hello" print (t1["a"]) t2 = nil--now only T1 is still referencing Tableprint (t1["a"]) T1 = nil--There is no reference to table now

The output is as follows:

Hihellohello

when the reference to a table is 0 o'clock, the LUA garbage collector eventually deletes the table and frees the memory space it occupies. When an element of table is not initialized, its content is nil, and as with global variables, nil is given to an element of table to delete the element.
In Lua, this form of a["name" provides a simpler way to write, and you can enter a.name directly.

We need to understand the difference between A.name and A[name], A.name represents a["name", which indicates that the table is indexed with the string "name", whereas A[name] is indexed by the value of the variable name, as in the following example:

Local A = {}name = "Lisa" a[name] = "Lilei" Print (A[name])--equivalent to a["Lisa"]print (A.name)--equivalent to a["name"]print (A.lisa)--equivalent to a[ "Lisa")

The output is:

Lileinillilei

?, 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 for using this operator, such as the following code:

Local A = {}a[1000] = 1print (#a)

The output is:

0

in Lua, the index result for all uninitialized elements is nil. Lua takes nil as a flag that defines the end of an array. When an array has "voids", that is, when the middle contains nil, the length operator considers these nil elements to be the end tags. Because a[1] = nil, the output for the above code should be 0. Therefore, this is a problem that needs to be considered when working with table. 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] = 1print (TABLE.MAXN (a))

The output is:

1000

function (functions),UserData (custom type), and thread (thread)

These three types will be summarized in a later

Types and values in Lua

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.