Lua sorting-table database and lua sorting table

Source: Internet
Author: User

Lua sorting-table database and lua sorting table
Table attributes

Table library is composed of some auxiliary functions.The function uses table as an array.. There are functions for inserting and deleting elements in the list, functions for sorting array elements, and functions for linking all strings in an array.

  • 0. In table. getn () Lua, we often assume that the array ends at the last non-nil element.

This traditional convention has one drawback: Our array cannot have nil elements. For most applications, this restriction is not a problem, for example, when all arrays have fixed types. However, sometimes our array needs to have nil elements. In this case, we need a method to explicitly indicate the size of the array.

  • 1. tabel. insert ()It is used to insert an element to the specified position of an array. It moves subsequent elements to empty space and increases the length of the array by 1.

Eg: if a is an array {, 30}, after calling table. insert (a,), a becomes {15, 10, 20, 30 }. A special case is that if we call insert without the location parameter, the element will be inserted at the last position of the array (so the element does not need to be moved ).

  • 2. table. remove ()The function deletes the element at the specified position in the array and returns this element. All the elements following it are moved forward and the size of the array changes.The last element of the array is deleted when no location parameter is called.

Using these two functions, it is easy to implement stack, queue, and double-end queue. The initialization structure is a = {}. A push operation is equivalent to table. insert (a, x), and a pop operation is equivalent to table. remove (). To insert an element at the end of the structure, we use table. insert (a, 1, x). to delete an element, use table. remove (a, 1 ). The last two operations are not particularly effective because they must move elements back and forth. However, because the table library functions are implemented in C, the efficiency of small arrays (hundreds of elements) is not a problem.

  • 3. table. sort () has two parameters:Array and sorting function for storing Elements.

The sorting function has two parameters. If the first parameter after sorting in array is before the second parameter, the sorting function must return true. If no sorting function is provided, sort uses the default smaller than operator for comparison. The default value is ascending.

A common error is the attempt to sort the table's lower-level domain. In a table, all subscripts form a set but are unordered. If you want to sort them, you must copy them to an array and then sort the array.

ForFor Lua, arrays are also unordered.. But we know how to count, so as long as we use sorted subscript to access the array, we can get the sorted function name. This is why we always use ipairs instead of pairs to traverse the array.The former uses the key sequence 1, 2 ,...... (Ipairs), the natural storage sequence of the latter table (pairs).

Table Method in LUA

Table is a table.

If you want to ask the table to have related operation functions

Let me talk about some common ones.

Table. sort ()
Table. insert ()
Table. remove ()
Table. concat ()
1 sort 23 insert and delete 4 string connections

Others are useless.

What are the values of tables in LUA?

Table. concat (table, sep, start, end)

Concat is the abbreviation of concatenate (chained, connected. table. the concat () function lists all elements in the array part of the specified table in the parameter from the start position to the end position, and the elements are separated by the specified separator (sep. Except table, other parameters are not required. The default Delimiter is a null character, the default start value is 1, and the default end value is the total length of the array.

The sep, start, and end parameters are read in sequence. Therefore, although they are not required, to specify backend parameters, you must specify the preceding parameters.

> Tbl = {"alpha", "beta", "gamma "}
> Print (table. concat (tbl ,":"))
Alpha: beta: gamma
> Print (table. concat (tbl, nil, 1, 2 ))
> Print (table. concat (tbl, "\ n", 2, 3 ))

Table. insert (table, pos, value)

The table. insert () function specifies an element where the position (pos) is inserted into the value in the array part of the table. The pos parameter is optional. The default value is the end of the array part.

> Tbl = {"alpha", "beta", "gamma "}
> Table. insert (tbl, "delta ")
> Table. insert (tbl, "epsilon ")
> Print (table. concat (tbl ,",")
Alpha, beta, gamma, delta, epsilon
> Table. insert (tbl, 3, "ETA ")
> Print (table. concat (tbl ,",")
Alpha, beta, alpha, gamma, delta, epsilon

Table. maxn (table)

Table. the maxn () function returns the maximum value of all positive key values in the specified table. if no element with a positive key value exists, 0 is returned. this function is not limited to the array part of the table.

> Tbl = {[1] = "a", [2] = "B", [3] = "c", [26] = "z "}
> Print (# tbl)
3 -- because the 26 and the previous numbers are not consecutive, they are not included in the array.
> Print (table. maxn (tbl ))
> Tbl [91.32] = true
> Print (table. maxn (tbl ))

Table. remove (table, pos)

Table. the remove () function deletes and returns the elements in the pos position of the table array. the subsequent elements are moved forward. the pos parameter is optional. The default value is the table length, that is, the last element is deleted.

Table. sort (table, comp)

Table. sort () ...... remaining full text>

Related Article

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: 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.