"Programming in Lua 3" study notes (c)

Because some time ago were back to school thesis defense, it did not update the blog for a long time. Graduated, then the Gansha just do it, no graduate unemployment, it should be a little glad about it.

The second reading before the second point to note-taking penultimate chapter, leaving no record of table-related. Contact Lua friends said, a lot of time around the table in Lua is to write something, it shows the importance of table. The second chapter introduces the general usage under the Table, on this record it.

2.5 Table

First, the first point to note is that, Table of the index is very flexible, the book also introduced not only to the table index is a subscript index can also be used in addition to Lua types of nil supported as an index, so here It reflects its flexibility.

Table form:

a = {}

Some usage on Table:

a = {y = 10}   --一个table,键=y,值=10
x = "y" --
--注意这里的索引方式,这里很容易混淆
print(a[x])
print(a.y)
print(a["y"])

As can be seen from the above Table flexibility of use, its index on the way I feel a little bit confusing. In this way one kind stupid I use: If the "[]" index this manner, then the "[]" which would have to get a value in double quotes is a bond, may be [Y], or x = "y"; "." [x], if the index using this method, it is directly behind the dot bond table.


Can use the "=" Table a variable passed to another (not know what to say in the end), as follows:

a = {x = 10}
b =  a
print(b.x)  --10

Support features such as flexible syntax to use Table laid the foundation for this knowledge later have to review again, how the expression is not very clear.

 

When we do not need a table, it can automatically reclaim memory by giving it a value nil to the system, to be noted here:

a = {x = 10}
b = a
a = nil
--在将nil赋值给a之后,系统回收a占用的内存,但此时b依旧可以使用,即可以使用b.x
print(b.x) --10
print(a.x) --error


When using the Table, unnecessary application memory size, you can just use according to their needs.


Note that if we use the numbers for the index, Table 1 is the start of the index.


A method to obtain the length of the table: #


2.6 Function

Function of knowledge about the contents of the follow-up detailed introduction, roughly feel the need to note that its return value can have more , with Lua Lua support calling functions written and functions written in C.


2.7 Userdata and Threads

These two points in the second chapter is not too much introduction, it will be referred to the detailed description in subsequent sections, then later in the Well.

 

Reproduced in: https: //www.cnblogs.com/zhong-dev/p/4044587.html

Guess you like

Origin blog.csdn.net/weixin_33912445/article/details/94600948