Pairs and ipairs differences in LUA

Source: Internet
Author: User
Ipairs and pairs are all functions that traverse Tbale in Lua, but the two are different.

1.pairs iterates through all the key-vale in the table and ipairs increments the corresponding table[i] value from 1 and 1 based on the value of the key.

Pairs can traverse all the keys in the table and can return nil in addition to the iterator itself and the traversal table itself; However, Ipairs cannot return nil, only the number 0 can be returned, and if nil is encountered, exit. It can only traverse to the first key that appears in the table that is not an integer

A = {[1] = "A1", [2] = "A2", [3] = "A3", [5] = "A4", [6] = "A5",}for key, value in Ipairs (a) do  print (key, value) end result : 1a12a23a3a = {[1] = "A1", [2] = "A2", [3] = "A3", [5] = "A4", [6] = "A5",}for key, value in pairs (a) do  print (Key, VA Lue) End result: 6a52a23a31a15a4


Pairs and ipairs differences in LUA

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.