Lua for cyclically traverses several table differences: luatable

Source: Internet
Author: User

Lua for cyclically traverses several table differences: luatable

It is well known that there are two types: ipairs and pairs. I believe you are familiar with these two types. ipairs traverses them sequentially from the beginning (idx = 1, in the case of nil, the loop is exited; in the case of pairs, the loop is non-sequential traversal, as long as there are values in it, it can be traversed.

So what if I need to traverse in sequence and even if there is nil in the middle, it can be completely traversed to the end?

Next let's talk about this: Use maxSize = table. maxn (pTable) to get the location of the last element of the table (or the maximum length, including nil), and then:

For idx = 1, maxSize do if pTable [idx] ~ = Nil then -- perform corresponding processing... endend

In this way, both sequential traversal and full traversal can be achieved.

Related Article

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: 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.