(Original) cocos2d-x 3.0 + Lua learning and work (5): remove the pit of table

Source: Internet
Author: User

In this chapter, we will focus on table removal. If this item is not noticed, it will be easily pitted (when it passes through ~~~), Of course, this is for the array table. Use the ipairs Method for traversal.

Xingyue's contribution ~~~

See example 1: Direct Nil

Local TBL = {1, 2, 3, 4, 5} for K, V in ipairs (TBL) do if v = 3 then TBL [k] = nil else print ("One :".. k .. ":".. v) end -- what will be output again... For K, V in ipairs (TBL) Do print ("two ".. k .. ":".. v) end output: One: 1: 1one: 2: 2one: 4: 4one: 5:5 -- no output 3: two1: 1two2: 2 -- only output, 1, 2.

As you can see, when the output is again, only 1 and 2 are output. (TOM: Why ...)

After direct nil, the table element becomes {[1] = 1, [2] = 2, [4] = 4, [5] = 5 }, I already know that ipairs is continuously traversed. If search 3 does not exist, the traversal ends. (TOM: Yes ~~)

Directly nil, the key values of other elements in the table will not change ~~~ (Pit ...)

Is there a way to rearrange the table elements after deletion?

This is the second method I have not mentioned: Table. Remove ()

Example 2: delete table. Remove

Local TBL = {1, 2, 3, 4, 5} for K, V in ipairs (TBL) do if v = 3 then table. remove (TBL, k) else print ("One :".. k .. ":".. v) end -- what will be output again... For K, V in ipairs (TBL) Do print ("two ".. k .. ":".. v) end output: One: 1: 1one: 2: 2one: 4: 5 -- 1. note: here only vlaue: 5 -- no output 3 is output again: two1: 1two2: 2two3: 4 -- 2. note: Key: 3two4: 5 -- 3. note: Key: 4 -- after the output is made again, the key value is continuous.

We can see that all elements are output and the key value is continuous. This is the benefit of the table. Remove Delete method. This is correct ~~~

Now we have a problem. (TOM: What's the problem ...)

In the first output, the comment shows that only 5 is output and 4 is not output. (TOM: What... value: 4 ???)

This is another pitfall ~~~~ (Xiao Bai: haokeng ~~~) You think it traverses everything, but it actually misses people...

The reason is:

Table. Remove is automatically sorted. The removed TBL values are {1, 2, 4, 5}

However, in the first for loop, K = 3 at the time of removal, K = 4 when entering the next statement again, then TBL [4] = 5, then K reaches the maximum value and ends the loop. Therefore, only 5 is output, and 4 is not output.

TIPS: this bug is very simple. After I remove it, Let k = k-1; then, when I enter the next statement again, K is equal to 3, and 4 is output.

Xingyue: Right? (TOM: Isn't it ...?)

In order to let Tom die, analyze the value of K here.

The value of K is the value returned by the ipairs iterator. Even if K is set to k-1, that is, the iterator returns 4 for the next call, k = 4. Therefore, changing the value of K is meaningless. (Try again ~~~~)

(Xiao Bai: haokeng ~~~ What should we do ???)

So how can we solve this problem?

 

Example 3: reverse search, and then call table. Remove

Local TBL = {1, 2, 3, 4, 5} For k = # TBL, 1,-1 do if TBL [k] = 3 then table. remove (TBL, k) else print ("One :".. k .. ":".. TBL [k]) end -- what will be output again... For K, V in ipairs (TBL) Do print ("two ".. k .. ":".. v) end output: One: 5: 5one: 4: 4one: 2: 2one: -- all the elements are output here !! -- No output 3 Output again: two1: 1two2: 2two3: 4 -- 2. note: Key: 3two4: 5 -- 3. note: Key: 4 -- after the output is made again, the key value is continuous !!

You can output all elements for the first time and all elements for the second time. (Xiao Bai: Zhenye ~~~) (Xingyue: vomiting ...)

We analyzed it by ourselves, similar to the principle in example 2.

 

This chapter ends with hope to help you.

Xingyue's contribution ~~~

 

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

(Original) cocos2d-x 3.0 + Lua learning and work (5): remove the pit of table

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.