How table is constructed

Source: Internet
Author: User

Http://www.jb51.net/article/55115.htm

0.Lua Debugging Tools--luaeditor

First, if you are in touch with Lua for the first time, please add the basics of Lua's basic syntax (Jor: o O!). ), and then download a luaeditor tool to see how the LUA performs and, of course, to debug, this article does not explain the tool. Can Baidu a bit of this tool.

1. What is a table?

Table is the most complex and powerful data structure of LUA, and Lua is not an object-oriented language in itself, but programmers who are deep in object-oriented poisoning can simulate object-oriented programming with table "perfect". Most simply, we can understand the table as an array, the most complex of which we can think of as "everything in the world," because it can create a lot of things you can't imagine. A word, freedom is very big ~! (Jor: A word, your sister! )

2. How do I create a table?

Create TABLE is a very complex thing, do not know that the top can not stay, try, as follows:

Copy CodeThe code is as follows:
Local A = {}


This creates a table.
(Jor: Poof, it's complicated ...) Ah, your sister, complex you! )

3. How to initialize a table

Aw, although creating a table is already complex, more complex is still behind, how to initialize table? Take a look at the following code:

Copy CodeThe code is as follows:
Local A = {["x"] = 3, ["Mutou"] = A, [] = "Hello"}
Print (a["x"]);


Create a LUA file in Luaeditor, enter the above code, save, and press F5 to run, we will see the Output window output a number: 12.

It's amazing, it feels like you're defining an array, isn't it?

The elements between table are separated by commas, ["x"] = 12 for constructing a TABLE element, labeled "X", and a value of 12. (Jor: The subscript for a hair array can be a string?) )

Aw! I'll just wait for the narrator to ask the question, Narrator: You idiot, I'm only saying table like an array, I'm not saying it's an array, table supports almost all types of subscripts, including functions.

(Jor: ...) I will not be in the future, Meow ... )

4. There is a more concise way

This type of initialization seems too complex, is there any simple point? Take a look at the following code:

Copy CodeThe code is as follows:
Local A = {x = 3, Mutou = in-the-.
Print (a["x"]);


The running result is still output 12, that is, for the string subscript, we can omit the box and double quotation marks, but the number subscript can not, do not confuse.

But a["x" is annoying, isn't it? Programmers are lazy, so LUA supports this way of calling:

Copy CodeThe code is as follows:
Local A = {x = 3, Mutou = in-the-.
Print (a.x);


The output is still 12, that is, for the string subscript, can be called directly in the form of a.x, but also omitted the box and double quotation marks, the initialization and invocation of the same rules, so that everyone will not confuse, remember, is the string subscript to do so.

Remember, it is the string subscript to do so.

Remember, it is the string subscript to do so. (Jor: You try again, I promise I will not help you spit groove.) )

5. What if I want the default digital index?

If you say, you are accustomed to the array, with the digital subscript, and do not want to define their own numbers, such as:

Copy CodeThe code is as follows:
Local A = {[1] = 12, [2] = 43, [3] = 45, [4] = 90}


It doesn't matter, LUA also provides a trick for lazy programmers, as follows:

Copy CodeThe code is as follows:
Local A = {12, 43, 45, 90}
Print (a[1]);


See a[1] Output value is not 12? (Jor: But the subscript for 12 should be 0, and the array subscript is calculated starting from 0)

Once again, the table is similar to an array, but it is definitely not an array.

Table defaults to the first index subscript is 1, and many related functions also assume that the first index of table is labeled 1, so it is best to follow this rule.

6. Table of the more powerful application table

Table can be put in everything, and of course it can be put on table, as follows:

Copy CodeThe code is as follows:
Local A = {
{x = 1, y = 2},
{x = 3, y = 10}
}


We define a table, named A,table, with two elements, two with no names, and in accordance with the rules in 5th, the specified subscript is not displayed, and Lua will default to the table element with subscript (starting from 1).

Therefore, the first element of a is {x = 1, y = 2}, and the first element of a is called: a[1]
Because A[1] is another table, the value of the x below the table is called again: a[1].x
This will be OK.

7. More powerful application of the function subscript

We most later try to play better, using a function as the Subscript for table:

Copy CodeThe code is as follows:
function test ()
Print ("Hello Table");
End
Local A = {[Test] = 1}
Print (A[test]);


Defines a function test, and then uses test as the subscript to define an element of table with a value of 1.
It's strange, isn't it? But sometimes it's really useful, and it's not explained for the time being.

How table is constructed

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.