In Pairs or Ipairs

3

Are there any differences between in ipairs and in pairs ? What?

a = {1, "b", 3, "d"}
for i, v in pairs(a) do print(i, v) end
print("Ipairs")
for i, v in ipairs(a) do print(i, v) end

Return:

1   1
2   b
3   3
4   d
Ipairs
1   1
2   b
3   3
4   d
    
asked by anonymous 03.11.2014 / 04:16

1 answer

6

ipairs cycles through the entries with integer indexes in a table, in order.

pairs traverses all entries in a table, in arbitrary order.

In your example, add a.teste=true after creating the table or teste=true at creation. %% Will be visited by teste,true but not pairs .

    
03.11.2014 / 09:33