Lua (5)-table (table)

Source: Internet
Author: User
Tags constructor numeric lua sorts
Table is the only data structure in LUA, and table allows you to implement modules, packages, and objects (object) representations. For example, IO in Io.read is a module, and read is an index in the IO module (using the string "read" as the index of the Read method). Only one reference to Tabel is held in the program (just like a static variable). (1) The constructor of table. a stereotype is an expression that is used to create and initialize a table. The simplest constructor is an empty constructor {}, which is used to create an empty table. You can also construct a table with elements, such as TBL = {"A", "B", "C"}, as you would initialize an array in the C language. Constructor 1 (array construction): local TBL = {"A", "B", "C"} on the formula equivalent to local TBL = {} Tbl[1] = "A" tbl[2] = "B" tbl[3] = "C" is also equivalent to local TBL = {[1] = "A", [2] = "B", [3] = "C"} using an array to construct a table allows table to have an array-like nature, one notable feature is that you can use the numeric index of the element to find the corresponding value, or you can use the For loop to iterate over each element. However, the value of an element cannot be obtained through a non-numeric index.
Output
Stereotype 2 (Specifying index constructs ):
Local TBL = {a = "a", B = "B", C = "C"} is equivalent to local TBL = {} TBL.A = "a" tbl.b = "B" tbl.c = "C" Specifies that a variable is indexed allows the table to implement a key similar to map Value-to-find functionality, which cannot be indexed to the value of an element by using Tbl[index], or to the number of elements of a table using #tbl, TABLE.MAXN, or TABLE.GETN, and iterating over the elements in the TBL requires the use of iterations (for k,v in Pairs (TBL) do ... end) to implement the method.
Output
stereotype 3 (mixed nesting Construction ):
Local TBL = {1,2,a = "a", B = "B", 3,4,{x = 1,b = 2}} using a structure similar to the above, allows the table to have both the constructor 1 and the constructor 2 function, but when using tbl[index], if index is valid to find To the value of the corresponding element.
Output
regardless of the structure used, LUA allows users to modify and delete the table after it is defined, without having to manually request new memory. Like what
Output
(2) a reference to table. When a program no longer references a table, the Lua garbage collector (garbage collector) eventually deletes the table and re-uses its memory.
Output
(3) A weak reference to table.
LUA has an auto-recycle feature that automatically removes objects in memory that are considered to be memory garbage (such as addresses that are not referenced). In the table, Without any processing of the elements in the table, even if some elements in the table are not used anywhere, they will not be recycled by the LUA GC collector because they are referenced by the table. The weak reference table (weak table) of LUA is such a mechanism that users can use to tell Lua that a reference to a table should not hinder the collection of an object. In Lua, the use of "values" (such as numbers, strings) and "Boolean" (Boolean) as the key to table cannot implement a weak reference to table; Using object types such as table and Userdada as key can implement weak references to table.
Output
As you can see, when you do not set a weak reference to TBL, even if the value of "A" is indexed by C, it will no longer be found and will not be recycled. Set a weak-referenced meta-table for TBL, and then look at the effect.
Output
As you can see, with the weak reference to table, LUA can recycle even if the elements in the table become memory garbage. If you replace C with number or string type, you will not be able to set a weak reference for TBL through the key value, in which case the Tbl[key] data will be reclaimed by LUA only if the value of Tbl[key] is nil. (4) table can access the values of its elements with different types of indexes, and the table will grow automatically when a new entry is to be accommodated.
Can also be written like this
Output
(5) Note the difference between a.x and a["X". a["x" means the table is indexed with the string "X", which is indexed by the value of the variable x.
Output
(6) Use table to represent a traditional array or linear table (table in Lua is the index value starting with the following superscript 1).
Output
You can use the # symbol to represent the index value of the last element in the array, such as
Output
(7) The size of the array is defined. The length of the array in Lua is determined by nil, and when an element in the array is nil, LUA considers this to be the end tag of the array, so it is safe to use # Most of the time, but it is sometimes important to note if an element is nil in the array, If the elements of the array are not necessarily stable, use TABLE.MAXN to return the maximum number of positive indexes for a table
Output
(8) The connection of each element in the Table.concat (table,sep,start,end) array. LUA provides a library function for table.concat (table, Sep, start, end) to support the connection of elements within a table. CONCAT is the abbreviation of concatenate (chain, connection). The Table.concat () function lists all elements from the start position to the end position of the array portion of the table specified in the parameter, separated by the specified delimiter (SEP). In addition to the table, other parameters are not required, the default value of the delimiter is a null character, and the default value of start is the 1,end default value is the total length of the array portion. Sep,start,end These three parameters are sequential-read, so although they are not required parameters, if you want to specify a back argument, you must specify both the previous parameter.
Output
(9) insertion of new elements of Table.insert (Table,pos,value). LUA provides the library function Table.insert (table, POS, value) to support the insertion of new elements. The Table.insert () function inserts a value of one element in the array portion of table specified position (POS). The POS parameter is optional and defaults to the end of the array part.
Output
TABLE.MAXN (table) safe array size definition. The TABLE.MAXN () function returns the largest key value in all positive key values in the specified table. If there is no element with a positive key value, 0 is returned. This function is not limited to the array portion of table.
Output
(one) table.remove (table, POS) array elements are deleted. The Table.remove () function deletes and returns an element of the table array part at the POS location, followed by the element being moved forward. The POS parameter is optional and defaults to the table length, which is the deletion from the last element.
Output
table.sort (table, comp) array of sorts. The Table.sort () function sorts the given table in ascending order. Comp is an optional parameter that is an external function that you can use to customize the sort criteria for the sort function. This function should meet the following criteria: Accept two parameters (A, B, in order), and return a Boolean value that returns True when a should be in front of B, and returns false instead. For example, when we need to sort in descending order, we can write a descending comparison function and use this comparison function for sort.
Output

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.