#Iterator Functions
There are 3 different types of Iterator Functions excluding custom ones. . pairs, ipairs and next.
#Basic For Loop
A Generic For Loop is a loop that keeps track of how many times it has looped. The difference between a Numeric For Loop and a generic one, is that we can use the iterator in our script; the iterator isn't just an index.
for i = 1,20 do
print(i)
end
1 2 3 4 ... 18 19 20
We can use this for loop to get the values of a table.
local table = {1, 2, 3, "Hi", true}
for i = 1, #table do
print(table[i])
end
1 2 3 Hi true
#Pairs
Pairs is the most common way people get values from a table.
local table = {1, 2, 3, "Hi", true}
for iteration,value in pairs(table) do
print(value)
end
What Pairs is doing is it is essentially going through every single object inside of the table and getting its value and iteration. It doesn't do it in numerical order. The reason it doesn't read the table in numerical order is in case the table is written as a dictionary. This is a dictionary:
local dictionary = {
[1] = 1,
["Hello"] = "world!"
}
The keys in a normal table go in numerical order. The first item in a table has a key of 1, the second has a key of 2 and etc... In a dictionary, keys can be a bool, string or etc. Using the same dictionary:
for _,v in pairs(dictionary) do
print(v)
end
Would print everything in the dictionary.
for i = 1, #dictionary do
print(dictionary[i])
end
Would print 1 and 1 nil because the table would try to call dictionary[2] which isn't set. dictionary["Hello"] is set, however.
#Ipairs
ipairs is similar to pairs. The i stands for integer. This means that while pairs can look at every single value in a dictionary, ipairs goes through the entire table and only looks for keys with consecutive integers starting from 1.
local dictionary = {
[1] = 1,
["Hello"] = "world!",
[2] = true,
[4] = "nil but a string",
[3] = "foo",
[6] = "bar"
}
for _,v in ipairs(dictionary) do
print(v)
end
1 true nil but a string foo
It prints the value of the keys 1-4 because they have consecutive keys starting from 1. The key of 5 is non-existent so it stops before it can print the value of 6. You could also think of it like this: It goes through the table from 1 to math.huge (basically infinity) until it gets a value of nil. That is why, even in normal tables, if there is a value of nil, it will stop at that key.
local table = {true, false, nil, 3}
for i,_ in ipairs(table) do
print("Inspecting item number "..tostring(i))
end
Inspecting item number 1 Inspecting item number 2
Key #3's value is nil. Ipairs will stop!
#Next
Next is used like so:
local table = {nil, 1, 2, true}
print(next(table))
for _,v in next,table do
print(v)
end
local a = {}
local b = {1}
print(next(a), next(b))
Next has 2 arguments. The first is the table and the second is what is called, "lastKey". What the second argument does is it gives you the next key and its value.
local table = {true, nil, false}
print(next(table, 0))
print(next(table, 2))
Next is not used as much as pairs or ipairs.
#TL;DR/Layman's Terms
Pairs, Ipairs and Next are all built-in global functions used in generic for loops for indexing through tables. They just work slightly differently.
Hope it helps!
#Info on Stateless Iterators
Stateless iterators on official lua site
1 comment
ui grid layout will help you A LOT in this case ^_^