(Original) cocos2d-x 3.0 + Lua learning and working (4): common function (5): returns all keys in the specified table (key): Table. Keys

Source: Internet
Author: User

The function here is mainly used to return all the keys in the specified table. Reference: quick_cocos.

Xingyue's contribution ~~~

--[[-- 返回指定表格中的所有键(key)-- example:    local t = ( a = 1, b = 2, c = 3 )    local keys = table.keys( t )    -- keys = { "a", "b", "c" }    -- @param t 要检查的表格(t表示是table)-- @param table 返回指定表格的所有键(key),它是一个键集合的表格--]]function table.keys( t )    local keys = {}    for k, _ in pairs( t ) do        keys[#keys + 1] = k    end    return keysend

The principle is simple: Get all the keys of the specified table, save the keys to a table, and then return. So easy ~~~ (Xiao Bai: Do not speak birds ...)
The function of getting all the keys seems useless... however, it can be used in special cases.

Before that, let's take a look at a simple sorting example... who throws vegetables... I... (TOM: You have today ...)

local tbl = { 1, 3, 5, 6, 5, 8, 0 }  -- 一个数组-- 星月要对这个数组排序table.sort( tbl )  -- 系统自带排序方法for k, v in ipairs( tbl ) do     print( k .. ":" .. v )end

There is an array table on it, And xingyue uses the sort function of table to sort and output the result. Note: ipairs traversal is used here. Because ipairs is sequential traversal ~~~

输出:1:02:13:34:55:56:67:8

There are no output errors.

(Xiao Bai: But I want to output from high to low ...) Xingyue: this can have...

local tbl = { 1, 3, 5, 6, 5, 8, 0 }  -- 一个数组-- 排序规则local function isBag( t1, t2 )    return t1 > t2end-- 星月要对这个数组排序table.sort( tbl, function( t1, t2 )    return isBag( t1, t2 )end )for k, v in ipairs( tbl ) do     print( k .. ":" .. v )end输出:1:82:63:54:55:36:17:0

Oh, too ~~ Output from large to small. (Xiao Bai: It looks amazing ...)
In the analysis, we first add an isbag function, which returns the comparison results of two values. Then an anonymous function (T1, T2) is added to sort, which returns the isbag result. When sorting TBL, the anonymous function is called. The input parameter is the value of TBL, Which is sorted according to the results returned by the anonymous function. Then, no. (Xiao Bai: Hao dizzy .)

 

The table. Sort () method can only sort arrays. Note This.

 

The above is to sort the values of the array. If you want to sort the key values, you can use the functions described in this chapter.

function table.keys( t )    local keys = {}    for k, _ in pairs( t ) do        keys[#keys + 1] = k    end    return keysendlocal tbl = { [101] = "a", [121] = "b", [186] = "c", [100] = "d", [145] = "e", [122] = "f", [11] = 0 }  -- 非数组tbllocal keys = table.keys( tbl )  -- 获得指定表格所有keys-- 排序规则local function isBag( t1, t2 )    return t1 > t2end-- 注意:星月对keys表格进行排序table.sort( keys, function( t1, t2 )    return isBag( t1, t2 )end )for k, v in ipairs( keys ) do     print( "keys: " .. v .. ":" .. tbl[v] )   -- 根据keys,输出tbl。对key排序。end输出:keys: 186:ckeys: 145:ekeys: 122:fkeys: 121:bkeys: 101:akeys: 100:dkeys: 11:0

Now, this chapter ends.

Key points of this chapter: The table. sort method and the table. Keys method.

(Xingyue: Xiao Bai ...)

(Xiao Bai: Do not disturb me to watch the antenna baby ~~~)

 

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/4022877.html

(Original) cocos2d-x 3.0 + Lua learning and working (4): common function (5): returns all keys in the specified table (key): Table. Keys

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.