In Pairs or Ipairs

Asked

Viewed 1,560 times

3

Is there any difference between in ipairs and in pairs? Which?

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

Returns:

1   1
2   b
3   3
4   d
Ipairs
1   1
2   b
3   3
4   d

1 answer

7


ipairs traverse entire index entries in a table, in order.

pairs scrolls through all entries in a table, in arbitrary order.

In your example, add a.teste=trueafter creating the table or teste=true at creation. The pair teste,true will be visited by pairs but not by ipairs.

  • 1

    Just to complement, I ran the test on http://ideone.com/eixTay

Browser other questions tagged

You are not signed in. Login or sign up in order to post.