The difference between pairs and ipairs in Lua _lua

Source: Internet
Author: User
Tags lua

Instructions on the official documentation:

Ipairs (t)

Returns three Values:an iterator function, the table T, and 0, so this construction

For i,v in Ipairs (t) does body end

Would iterate over the pairs (1,t[1]), (2,t[2]), and the up to the "the" "the", the "" "," "the" ".

Pairs (t)

Returns three values:the next function, the table T, and Nil, so this construction

For k,v in pairs (t) does body end

Would iterate over the all key–value pairs of table T.

The caveats of modifying the table during its traversal.

In this way we can see the difference between ipairs and pairs. Pairs can iterate through all the keys in the table, and can return nil in addition to the iterator itself and the traversal of the table itself, but Ipairs cannot return nil, can only return the number 0, and exits if nil is encountered. It can only traverse to the first key that appears in the table that is not an integer

Here's an example

Copy Code code as follows:

Local Tabfiles = {
[3] = "Test2",
[6] = "Test3",
[4] = "Test1"
}
For K, V-ipairs (tabfiles) do
Print (k, v)
End

Guess what it turns out to be? According to the analysis just now, it is in Ipairs (tabfiles) traversal, when Key=1 value is nil, so the direct jump out of the loop does not output any value.

Copy Code code as follows:

>lua-e "Io.stdout:setvbuf ' No" "Test.lua"
>exit code:0

Well, if it is

Copy Code code as follows:

For K, V-pairs (tabfiles) do
Print (k, v)
End

Will output all of the following:

Copy Code code as follows:

>lua-e "Io.stdout:setvbuf ' No" "Test.lua"
3 Test2
6 Test3
4 test1
>exit code:0

Now change the contents of the table:

Copy Code code as follows:

Local Tabfiles = {
[1] = "Test1",
[6] = "Test2",
[4] = "Test3"
}

For K, V-ipairs (tabfiles) do
Print (k, v)
End

Now the output is obviously the value of Key=1 test1

Copy Code code as follows:

>lua-e "Io.stdout:setvbuf ' No" "Test.lua"
1 test1
>exit code:0

Copy Code code as follows:

--[Example 1.]] --
Local TT =
{
[1] = "Test3",
[4] = "Test4",
[5] = "TEST5"
}

For i,v in pairs (TT) do--output "test4" "Test3" "Test5"
Print (Tt[i])
End

For i,v in Ipairs (TT) does--disconnect when output "Test3" k=2
Print (Tt[i])
End

--[Example 2.]] --
TBL = {"Alpha", "beta", [3] = "Uno", ["two"] = "DOS"}

For i,v in Ipairs (TBL) do--output top three
Print (Tbl[i])
End

For i,v in pairs (TBL) do--all output
Print (Tbl[i])
End

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.