What is the difference of pairs() vs. ipairs() in Lua?
Solution 1
pairs()
and ipairs()
are slightly different.
-
pairs()
returns key-value pairs and is mostly used for associative tables. key order is unspecified. -
ipairs()
returns index-value pairs and is mostly used for numeric tables. Non numeric keys in an array are ignored, while the index order is deterministic (in numeric order).
This is illustrated by the following code fragment.
> u={}
> u[1]="a"
> u[3]="b"
> u[2]="c"
> u[4]="d"
> u["hello"]="world"
> for key,value in ipairs(u) do print(key,value) end
1 a
2 c
3 b
4 d
> for key,value in pairs(u) do print(key,value) end
1 a
hello world
3 b
2 c
4 d
>
When you create an tables without keys (as in your question), it behaves as a numeric array and behaviour or pairs and ipairs is identical.
a = {"one", "two", "three"}
is equivalent to a[1]="one"
a[2]="two"
a[3]="three"
and pairs()
and ipairs()
will be identical (except for the ordering that is not guaranteed in pairs()
).
Solution 2
There is no array
-type in Lua, only table
s which might have consecutive elements starting from index 1.
The generic for-loop, in contrast to the numeric for-loop, expects three values:
- A callable
- A context-value it passes on
- An initial index-value
It calls the callable with context-value and index-value, storing all the returned values in the provided new variables. The first one is additionally saved as the new index-value.
Now some representative examples of callables for the loop:
-
ipairs(t)
returns a function, the tablet
, and the starting-point0
.
The function is the moral equivalent to:function ipairs_next(t, i) i = i + 1 var v = t[i] if v ~= nil then return i, v end end
Thus, all numeric entries starting at 1 until the first missing one are shown.
-
pairs(t)
either delegates tot
's metatable, specifically to__pairs(t)
, or returns the functionnext
, the tablet
, and the starting-pointnil
.next
accepts a table and an index, and returns the next index and the associated value, if it exists.Thus, all elements are shown in some arbitrary order.
There are no limits to how creative one can be with the function, and that is what vanilla Lua expects.
See "Bizzare "attempt to call a table value" in Lua" for an example of a user-written callable, and how some dialects react if the first value is not actually a callable.
Related videos on Youtube

Admin
Updated on October 02, 2020Comments
-
Admin about 2 years
In a for loop, what is the difference between looping with pairs() and ipairs()? This page uses both: Lua Docs
With ipairs():
a = {"one", "two", "three"} for i, v in ipairs(a) do print(i, v) end
Result:
1 one 2 two 3 three
With pairs():
a = {"one", "two", "three"} for i, v in pairs(a) do print(i, v) end
Result:
1 one 2 two 3 three
You can test it here: Lua Demo
-
Admin almost 4 yearsI’m new to the language and was wondering what each one is, what is each type of for loop? What is the callable, context-value, and index-value? What is ipairs_next, a function you made?
-
Admin almost 4 yearsI believe that the ordering is guaranteed with pairs(), and it will go up through the numeric indexes and then will go to the other non numeric values in a random order.
-
luther almost 4 years@BeastCoder2: At lua.org/manual/5.3/manual.html#pdf-next, it says "The order in which the indices are enumerated is not specified, even for numeric indices." What you describe might be common behavior in Lua, but it could change in any bugfix release.
-
Admin almost 4 years@luther Thank you for clearing that up! It may just have been what I was using to run the code.
-
cyclaminist almost 4 yearsI've also seen
pairs
iterating over consecutive integer indices beginning at 1 (and sometimes over non-consecutive integer indices) in order, but it's not guaranteed. -
glenn jackman almost 4 yearsAnother important point about
ipairs
is that it only loops over the first sequence of indices, starting at 1: it stops at the first gap. Considerfor i,v in ipairs({[1]='a', [2]='b', [4]='d'}) do print(i,v) end
andfor i,v in ipairs({[2]='a', [3]='b', [4]='d'}) do print(i,v) end
-
Karambit almost 2 yearsi am new to lua too but i think that it is an iterator function, lookup "iterators in programming"