Lua basics: Several table Construction Methods

Source: Internet
Author: User

Lua basics: Several table Construction Methods

The previous research on Lua was on paper, and there was no real project trainer. Now the company's projects are basically developed with Lua. It's time to add my poor Lua knowledge.

I will not talk about basic data types, expressions, and loop structures. This simple thing will also be sprayed.

Today I want to talk about several table construction methods.

0. Lua debugging tool-LuaEditor

First, if you are in contact with Lua for the first time, please add the basic syntax of Lua: O !), Download a LuaEditor tool to view the Lua execution result. You can also debug it. This article does not explain this tool. : Http://www.bego.cc/file/22357242 Of course, you can also Baidu.

1. What is table?

Table is the most complex and powerful data structure of Lua. Lua itself is not an object-oriented language, but it is a programmer with deep object-oriented poisoning, table can be used to simulate Object-Oriented Programming perfectly. In the simplest way, we can think of a table as an array. The most complex thing is that we can think of a table as "everything in the world", because it can create a lot of things you cannot imagine. A word with a high degree of freedom ~!

2. How to Create a table?

Creating a table is a complicated task. I don't know if you can't afford it. Try the following: local a = {}

 
  1. local a = {} 

In this way, a table is created.

3. How to initialize a table

Even though creating a table is complicated and more complicated, how can we initialize a table? Take a look at the following code:

 
  1. local a = {["x"] = 12, ["mutou"] = 99, [3] = "hello"} 
  2. print(a["x"]); 

Create a lua file in LuaEditor, enter the above Code, save it, and press F5 to run it. We can see that the output window outputs a number: 12.

This is amazing. It feels like defining arrays, isn't it?

The elements of a table are separated by commas (,). ["x"] = 12 indicates that a table element is constructed. The subscript is "x" and the value is 12.

Success ~! I will wait for the narration to ask this question. You are stupid. I just say that table is like an array. I didn't say it is an array. table supports almost all types of subscripts, including functions.

4. More concise methods

This initialization method looks too complicated. Is it easy? Take a look at the following code:

 
  1. local a = {x = 12, mutou = 99, [3] = "hello"} 
  2. print(a["x"]); 

The running result is still OUTPUT 12. That is to say, for the string subscript, We can omit the box and double quotation marks, but the number subscript is not acceptable. Do not confuse it.

However, the call method a ["x"] is also annoying, isn't it? Programmers are very lazy, so Lua supports calling this method:

 
  1. local a = {x = 12, mutou = 99, [3] = "hello"} 
  2. rint(a.x); 

The output result is still 12. That is to say,. the box and double quotation marks are also omitted for calling in the form of x. The Initialization is the same as the calling method rules, so that everyone will not be confused. Remember, this is the case only when the string subscript is used.

Remember, this is the case only when the character string subscript is used.

Remember, this is the case only when the character string subscript is used.

5. What should I do if I want to default a digital index?

If we say that we are used to arrays and use numbers to subscripts, we don't want to define them one by one. For example:

 
  1. local a = {[1] = 12, [2] = 43, [3] = 45, [4] = 90} 

It doesn't matter. Lua offers a trick for the cool programmers, as shown below:

 
  1. local a = {12, 43, 45, 90} 
  2. print(a[1]); 

Check whether the output value of a [1] is 12?

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

By default, the first index subscript of a table is 1, and many related functions assume that the first index subscript of a table is 1. Therefore, we recommend that you follow this rule.

6. More powerful application of table

All tables can be put, and of course tables can be put, as shown below:

 
  1. </pre> 
  2. local a = { 
  3.  {x = 1, y = 2}, 
  4.  {x = 3, y = 10} 
  5. } 
  6. print(a[1].x); 
  7. <pre> 

We have defined a table named a. The table has two elements: two tables without names. According to the rules described in Article 5th, no specified subscript is displayed, then Lua will assign the subscript to the table element from 1 by default ).

Therefore, the first element of a is {x = 1, y = 2}. The method for calling the first element of a is as follows: a [1].

Because a [1] is a table, the following value of table x is called again: a [1]. x.

In this case, OK.

7. More powerful application function subscript

Finally, let's try something more interesting. Use a function as the subscript of the table:

 
  1. </pre> 
  2. function test() 
  3.  print("Hello Table"); 
  4. end 
  5.   
  6. local a = {[test] = 1} 
  7. print(a[test]); 
  8. <pre> 

Defines a function test and uses test as the subscript to define an element of the table. The value is 1.

This is strange, isn't it? But sometimes this is really useful. I will not explain it here.

OK. For the table construction method, it's a little tired and good night.

This article comes from [Dumb and Game Development]

Address: http://www.benmutou.com/archives/627

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.