Describes the concept of a table in Lua and its related operations.

Source: Internet
Author: User

Describes the concept of a table in Lua and its related operations.

This article mainly introduces the concept of a table in Lua and its related operation methods. It is the basic knowledge in Lua's getting started learning. For more information, see

Table is the only data structure in which Lua can help us create different types, such as arrays and dictionaries. Lua uses an associated array and a zero-string index with different numbers. Tables do not have a fixed size and can grow as needed.

All statements used by Lua, including representative packaging tables. When we access a method string. Format, which means that we are accessing the string encapsulation of the Formatting Function.

Representation and usage

Tables are called objects and they are neither worthwhile nor changeable. Lua creates an empty table using the constructor expression. It is important to know that there is no fixed relationship between the reference for saving the table and the variables of the table itself.

The Code is as follows:

-- Sample table initialization

Mytable = {}

-- Simple table value assignment

Mytable [1] = "Lua"

-- Removing reference

Mytable = nil

-- Lua garbage collection will take care of releasing memory

When we have a table and a set element, if we specify it as B, both a and B point to the same memory. No separate memory is allocated to B. If it is set to none, the table will still be accessible to B. When no table is referenced, the garbage collection process in Lua needs to be cleared to reuse the unreferenced memory.

An example is as follows to describe the preceding features of the table.

The Code is as follows:

-- Simple empty table

Mytable = {}

Print ("Type of mytable is", type (mytable ))

Mytable [1] = "Lua"

Mytable ["wow"] = "Tutorial"

Print ("mytable Element at index 1 is", mytable [1])

Print ("mytable Element at index wow is", mytable ["wow"])

-- Alternatetable and mytable refers to same table

Alternatetable = mytable

Print ("alternatetable Element at index 1 is", alternatetable [1])

Print ("mytable Element at index wow is", alternatetable ["wow"])

Alternatetable ["wow"] = "I changed it"

Print ("mytable Element at index wow is", mytable ["wow"])

-- Only variable released and not table

Alternatetable = nil

Print ("alternatetable is", alternatetable)

-- Mytable is still accessible

Print ("mytable Element at index wow is", mytable ["wow"])

Mytable = nil

Print ("mytable is", mytable)

When we run the above program, we will get the following output:

The Code is as follows:

Type of mytable is table

Mytable Element at index 1 is Lua

Mytable Element at index wow is Tutorial

Alternatetable Element at index 1 is Lua

Mytable Element at index wow is Tutorial

Mytable Element at index wow is I changed it

Alternatetable is nil

Mytable Element at index wow is I changed it

Mytable is nil

Table operations

Built-in functions in Table operations and their columns in the following table.

Let's take a look at some examples of the above functions.

Table concatenation

We can use the concat function to connect two tables, as shown below.

The Code is as follows:

Fruits = {"banana", "orange", "apple "}

-- Returns concatenated string of table

Print ("Concatenated string", table. concat (fruits ))

-- Concatenate with a character

Print ("Concatenated string", table. concat (fruits ,","))

-- Concatenate fruits based on index

Print ("Concatenated string", table. concat (fruits, ",", 2, 3 ))

When we run the above program, we will get the following output:

The Code is as follows:

Concatenated string bananaorangeapple

Concatenated string banana, orange, apple

Concatenated string orange, apple

Insert and delete

Insert a project into a table and remove the most common table operations. It is explained below.

The Code is as follows:

Fruits = {"banana", "orange", "apple "}

-- Insert a fruit at the end

Table. insert (fruits, "mango ")

Print ("Fruit at index 4 is", fruits [4])

-- Insert fruit at index 2

Table. insert (fruits, 2, "grapes ")

Print ("Fruit at index 2 is", fruits [2])

Print ("The maximum elements in table is", table. maxn (fruits ))

Print ("The last element is", fruits [5])

Table. remove (fruits)

Print ("The previous last element is", fruits [5])

When we run the above program, we will get the following output:

The Code is as follows:

Fruit at index 4 is mango

Fruit at index 2 is grapes

The maximum elements in table is 5

The last element is mango

The previous last element is nil

Sort table

Sort tables usually need to be sorted alphabetically with the elements in the sort function table. This is an example.

The Code is as follows:

Fruits = {"banana", "orange", "apple", "grapes "}

For k, v in ipairs (fruits) do

Print (k, v)

End

Table. sort (fruits)

Print ("sorted table ")

For k, v in ipairs (fruits) do

Print (k, v)

End

When we run the above program, we will get the following output:

The Code is as follows:

1 banana

2 orange

3 apple

4 grapes

Sorted table

1 apple

2 banana

3 grapes

4 orange

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.