Lua - Table Iterator
Table is a versatile data structure in Lua and is used to create various types like array/dictionary or to create various data structures like stack, queue, linked list etc. A table can be in numerically indexed form or in a key-value pairs based associated form.
Based on type of table, We can use following types of inbuilt iterators to navigate through table entries.
next− this is an inbuilt function to give the next value from the table when invoked. This iterator is mainly used in generic for loop and for associated table.
ipairs()− this iterator uses default keys starting from 1 and is useful where table is not having any explicit keys. It is useful for numerically indexed tables.
pairs()− this iterator is useful when table is having explicit keys.
Let's see each one of the above functions to iterate values of a table.
Example - Using next function
next function accepts a table and index to return the next value of the table.
next(table, [, index])
Here next function returns the associated value from the table as per index. If index is nil, then first value is returned. Following example showcases the same−
main.lua
-- initialize a table of fruits with associative keys
fruits = {[2] = "banana",[4] = "orange",[6]="apple",[8] = "grapes"}
-- print the entries of table along with keys
for key,value in next,fruits do
print(key,value)
end
Output
When the above code is built and executed, it produces the following result −
8 grapes 2 banana 4 orange 6 apple
Example - Using pairs iterator
pairs iterator function returns key-value pairs and is mainly used in associative tables. Keys are preserved but order is not preserved. See the example below−
main.lua
-- initialize table of fruits
fruits = {[2] = "banana",[4] = "orange",[6]="apple",[8] = "grapes"}
-- print the entries of table along with keys
for key,value in pairs(fruits) do
print(key,value)
end
Output
When the above code is built and executed, it produces the following result −
8 grapes 2 banana 4 orange 6 apple
Example - Using ipairs iterator
pairs iterator function returns index-value pairs and is mainly used in tables without associative keys. See the example below−
main.lua
-- initialize table of fruits
fruits = {"banana","orange","apple","grapes"}
-- print the entries of table along with default keys
for key,value in ipairs(fruits) do
print(key,value)
end
Output
When the above code is built and executed, it produces the following result −
1 banana 2 orange 3 apple 4 grapes
Example - a Custom Table Iterator
Following is an example of a custom stateful iterator which accepts an table and return next value.
main.lua
-- Initialize an array
array = {11, 22, 33, 44, 55, 66}
-- return a stateful iterator to get values of collection passed
function elementIterator (collection)
local index = 0
local count = #collection
-- The closure function is returned
return function ()
index = index + 1
if index <= count
then
-- return the current element of the iterator
local value = collection[index];
return index, value
end
end
end
-- loop through the iterator
for index, value in elementIterator(array)
do
-- print the element
print(index, value)
end
Output
When we run the above program, we will get the following output−
1 11 2 22 3 33 4 44 5 55 6 66