The difference between ipairs and pairs when Lua generic for traverses table

Meow - Introduction

When you want to use the generic for to traverse the Lua table, there are two ways:

 for i,v in ipairs(tableName) do print(i,v); end for k,v in pairs(tableName) do print(k,v); end

There is only one difference between the two, that is, ipairs and pairs. What is the difference between them?

Try pears

Define three tables, and call them "pure array", "pure table", "miscellaneous table 1" and "miscellaneous table 2" according to their different contents. Please note that: This kind of address is very imprecise, because they are all tables, that is, tables.

 --'Pure array' testTab ={1,2,3,4,5}; --'Pure table' testTab1 = {a = 1, b = 2, c =3}; --'Miscellaneous table 1' testTab2 = {"zi", a = 5,b = 10, c = 15, "miao","chumo"}; --'Miscellaneous table 2' testTab3 = {"zi", a = 5,b = 10, c = 15, "miao",nil,"chumo"};

Ipairs and pairs traverse pure arrays

The code is as follows:

 for i,v in ipairs(tableName) do Print ("ipairs traverses pure arrays"); for i,v in ipairs(testTab) do print(i,v); end Print ("pairs traverse pure array"); for k,v in pairs(testTab) do print(k,v); end

The output result is:

 for i,v in ipairs(tableName) do Ipairs traverses pure array 1 1 2 2 3 3 4 4 5 5 Pairs traverses a pure array 1 1 2 2 3 3 4 4 5 5

There is no difference between the results of the first run and the results of the second run.

Ipairs and pairs traverse pure tables

The code is as follows:

 for i,v in ipairs(tableName) do Print ("ipairs traverses pure tables"); for i,v in ipairs(testTab1) do print(i,v); end Print ("pairs traverse pure tables"); for k,v in pairs(testTab1) do print(k,v); end

The output result is:

 for i,v in ipairs(tableName) do Ipairs traverses pure table Pair traverses the pure table c 3 a 1 b 2

When running for the first time, the ipairs group has no output, and the pairs have output.
Running for many times, the ipairs group has no output, and the pairs group has no output Output sequence changes That is, a, b, c output order changes.

Ipairs and pairs traverse miscellaneous table 1

The code is as follows:

 for i,v in ipairs(tableName) do Print ("ipairs traverse miscellaneous table 1"); for i,v in ipairs(testTab2) do print(i,v); end Print ("pairs traverse miscellaneous table 1"); for k,v in pairs(testTab2) do print(k,v); end

The output result is:

 for i,v in ipairs(tableName) do Ipairs traversal miscellaneous table 1 1 zi 2 miao 3 chumo Pairs traverses miscellaneous table 1 1 zi 2 miao 3 chumo b 10 c 15 a 5

First run, a group of ipairs No key value data is output , but all the "single" data is output, and the pairs group outputs all the data.
After running for several times, ipairs is the same as above, and the output order of "single data" of pairs group is unchanged, but the output order of key value data is changed.

Ipairs and pairs traverse miscellaneous table 2

The code is as follows:

 for i,v in ipairs(tableName) do Print ("ipairs traverse miscellaneous table 2"); for i,v in ipairs(testTab3) do print(i,v); end Print ("pairs traverse miscellaneous table 2"); for k,v in pairs(testTab3) do print(k,v); end

The output result is:

 Ipairs traversal miscellaneous table 2 1 zi 2 miao Pairs traverses miscellaneous table 2 1 zi 2 miao 4 chumo a 5 c 15 b 10

For the first time, IPAirs has the same set of results in Table 1, but In case of nil (empty), the following data will not be output The pairs are the same as the results in Table 1, and can continue to output subsequent values beyond nil.
After multiple runs, a set of results of ipairs will not change, and the output order of pairs will not change except for the "single data" output order.

Analysis and summary

According to the analysis of the above results, ipairs can only be used to traverse "pure array" type data. It is powerless for the key value form, and will be output in order according to the "array index". If it encounters nil (null), it will jump out of the loop directly. Presumably, the whole process of ipairs may be "index pairs".
Pairs can be used to traverse all types of data, and can skip nil and continue to output subsequent values. At the same time, it will output "array index" type data in order, and then randomly output key value type data.
Why is this? Reprint the explanation from tsbyj on csdn:

This is because table stores values in order, but it stores key value pairs according to the hash value of the key, not in alphabetical or numerical order.
For a, if you execute print (a [3]), the output will also be Good. That is to say, table does not give an index value to the key value.
That is to say, ipairs only prints the data with index values in the table in the order of index values, and does not care about the data without index values.
The pairs print according to the index value first, and then print its value according to the hash value of the key value pair after printing.
http://blog.csdn.net/tsbyj/article/details/47302611

In other words, the love of tricolor painting has become a pigeon again

References

1. [Head picture] [www.lua.org] www.lua.org lua logo

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/2738.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. Time House 11-21 20:15 reply

    Why are you bothering Lua again? Why don't you open a comment

    • hare 11-21 20:57 reply

      Because the Three Color Painting Love has not been updated... pigeons