Introduction to some of the auxiliary functions of table in Lua _lua

Source: Internet
Author: User
Tags lua

The table library is composed of auxiliary functions that manipulate the table as an array. There are functions that insert and delete elements in the list, functions that have array elements sorted, and functions that link all the strings in an array.

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

There is a drawback to this traditional convention: we cannot have nil elements in our array. This limitation is not a problem for most applications, such as when all the array has a fixed type. But sometimes our array needs to have nil elements, in which case we need a way to explicitly indicate the size of the array.

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

Eg: If a is an array {10,20,30}, after calling Table.insert (a,1,15), a changes to {15,10,20,30}. A special case that is often used is that we call insert without positional arguments, and the element is inserted at the last position of the array (so no element movement is required).

The 2.table.remove () function deletes the element at the specified position in the array and returns the element, all subsequent elements are moved forward, and the size of the array changes. When called without positional arguments, he deletes the last element of the array.

With these two functions, it is easy to implement stacks, queues, and two-terminal queues. We can initialize the structure to a={}. A push operation is equivalent to Table.insert (a,x), and a pop operation is equivalent to Table.remove (a). To insert an element at the end of the structure we use Table.insert (a,1,x), and delete the element with Table.remove (a,1). The last two operations are not particularly effective because they have to move elements back and forth. However, because the table libraries use C implementations for these functions, there is no problem with the efficiency of small arrays (hundreds of elements).

3.table.sort () He has two parameters: an array of elements and a sort function.

The sort function has two parameters and the sort function must return true if the first argument is sorted in array before the second argument. If no sort function is provided, sort uses the default less-than operator for comparison. The default is ascending.

A common mistake is to attempt to sort the subscript fields of a table. In a table, all subscripts form a set, but are unordered. If you want to sort them, you have to copy them to an array and then sort the array.

For LUA, arrays are also unordered. But we know how to count, so as long as we use a sorted subscript to access the array we can get the ordered function name. That's why we've been using ipairs instead of pairs to traverse the array. The former uses key order 1, 2 、...... (ipairs), the natural storage order of the latter table (pairs).

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.