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