A detailed explanation of the concepts of tables in LUA and their related operational methods _ruby topics

Source: Internet
Author: User
Tags arrays garbage collection lua

The table is the only data structure in which LUA can help us create different types, such as arrays and dictionaries. LUA uses associative arrays and can be not only numeric but also has different 0 string indices. Tables are not fixed in size and can grow as needed.

All the statements used by LUA, including the representative tables for packaging. When we access a string of methods. format, which means that we are accessing the formatting features of the string encapsulation.
Representation and Usage

Tables are called objects and they are neither worth nor changing. LUA creates an empty table using the constructor expression {}. It is to be known that there is no fixed relationship between the reference to the Save table and the variables of the table itself.

Copy Code code as follows:
--sample Table Initialization
MyTable = {}

--simple Table Value Assignment
mytable[1]= "Lua"

--removing Reference
MyTable = Nil
--Lua garbage collection'll take care of releasing memory

When we have a table with the elements of the collection, if we designate them as B,a and b all point to the same memory. No separate memory is allocated separately to B. When set to none, the table will still have access to B. When the tables are not referenced, then the cleanup process is required in the LUA garbage collection so that the unreferenced memory is reused again.

An example is shown below to illustrate the above characteristics of the table.

Copy Code code 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", 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", 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 get the following output

Copy Code code 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

Manipulate the built-in functions on the table and they are listed in the following table.

Let's look at some examples of the above functions.
table Concatenation

We can use the CONCAT function to connect, like the two tables shown below.

Copy Code code 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 get the following output

Copy Code code as follows:
concatenated string Bananaorangeapple
concatenated string banana, orange, apple
concatenated string Orange, apple

Inserting and deleting

Inserts the items in the table and removes the most common table manipulations. The explanations below it.

Copy Code code as follows:
Fruits = {"Banana", "orange", "Apple"}

--Insert a fruit at the end
Table.insert (Fruits, "mango")
Print ("Fruit at index 4", fruits[4])

--insert Fruit at index 2
Table.insert (fruits,2, "grapes")
Print ("Fruit at index 2", 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 get the following output

Copy Code code 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 require and sort the elements in a table of functions alphabetically. An example of this is shown in the following illustration.

Copy Code code 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 get the following output

Copy Code code 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.