They are all iterator functions, used in generic for
loops.
ipairs
iterates through the integer keys of a Table, starting at 1 and stopping when the next consecutive integer key has a value of nil
:
1 | local t = { "gradea" , 245 , false } |
2 | t [ 6 ] = "This string won't be reached" |
4 | for key, value in ipairs (t) do |
pairs
and next
are, effectively, the exact same thing. next
is faster, slightly, since pairs
uses next
internally. They iterate through all members of a Table, but in an undetermined order (between server instances, that is):
02 | local t = { someStringKey = false , [ 24 ] = 16 , [ privateKey ] = "swaggy" , 1325 , false , "anchored" } |
04 | for key, value in pairs (t) do |
09 | for key, value in next , t do |
These iterators work with Tables. I used 'random' data in my Tables, so sorry if that confused you.
ipairs
works, effectively, like this:
2 | local t = { "gradea" , 245 , false } |
3 | t [ 6 ] = "This string won't be reached" |
5 | while t [ index ] ~ = nil do |
That while loop will print the values of t
using only consecutive integer keys, starting at 1.
In my Table t
, keys 1, 2, 3, and 6 have defined values, so the while loop (and thus, ipairs), will print the values at t[1]
, t[2]
, and t[3]
and then stop because t[4] == nil
.