(Original) cocos2d-x 3.0 + Lua learning and work (4): common function (5): calculate the number of table elements: Table. Nums

Source: Internet
Author: User

The function here is mainly used to calculate the number of table elements. Reference: quick_cocos.

Xingyue's contribution ~~~

Let's look at a piece of code first.

local tbl = { [1] = 2, [2] = 2, [3] = 3 }print( "tbl length is " .. #tbl )

This code obtains the length of TBL, that is, the number of elements.

输出:tbl length is 3

There are three elements, no problem. We noticed that the subscript of the element is, that is, the key value is: 1, 2, 3. That is, TBL [1], TBL [2], and TBL [3]. If we modify the subscript, what will happen after the key is modified?

local tbl = { [1] = 2, [5] = 2, [3] = 3 }print( "tbl length is " .. #tbl )
输出:tbl length is 1

Can't you give me a few comments ~~~ (TOM: Why is it 1? Isn't it three elements ???)
The element length obtained by the # TBL method of table is only valid for the array and must start from 1 and be searched sequentially. Therefore, in the appeal, # TBL, search 1 is true, search 2. If no result is found, end the search. The returned result is 1.

Let's look at two examples:

local tbl = { [4] = 2, [5] = 2, [3] = 3 }print( "tbl length is " .. #tbl )输出:tbl length is 0
local tbl = { ["a"] = 2, ["b"] = 2, ["c"] = 3 }print( "tbl length is " .. #tbl )输出:tbl length is 0


 

Now go to the topic in this chapter to obtain the number of table elements. The Code is as follows:

--[[-- 计算表格包含的字段数量-- lua table 的 “#” 操作只对依次排序的数值下标数组有效,table.nums() 则计算 table中所有不为 nil 的值的个数。-- @param t 要检查的表格(t表示是table)-- @return integer 返回表格的字段数量--]]function table.nums( t )    local count = 0    local t = checkTable( t )    for k, v in pairs( t ) do        count = count + 1    end    return countend

Checktable is a function in my previous article. Link: http://www.cnblogs.com/wodehao0808/p/4015648.html.

The table. Nums method can be used to obtain the element. Attached example:

local tbl = { [4] = 2, [5] = 2, [3] = 3 }print( "tbl length is " .. table.nums( tbl ) )输出:tbl length is 3

This chapter ends now, Tom. Do you understand that there is no... (TOM: I have an IQ of 251, okay ....)
(Xingyue: Xiao Bai. You used to be 250 + 1)

(TOM :...)

 

The difference between the pairs and ipairs of the for loop is further illustrated at the end:

Pairs returns key and value. All elements in the table can be traversed. However, pairs traversal is random and unordered. (Xingyue: This feature has passed ...)

Ipairs traverses the array, similar to # TBL to obtain the number of elements. It also starts from 1 and is sequentially searched. For incremental search, ipairs is an ordered search.

 

This ends ~~~

 

The author uses cocos2d-x 3.0 + Lua learning and work experience, without the author's consent, please do not reprint! Thank you for your patience ~~~

This article is not approved by the author and cannot be reproduced. Otherwise, relevant responsibilities will be held accountable. For more information, see the source !!~~

Address: http://www.cnblogs.com/wodehao0808/p/4021921.html

(Original) cocos2d-x 3.0 + Lua learning and work (4): common function (5): calculate the number of table elements: Table. Nums

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.