Updated April 20, 2023
Introduction of Lua Next
Lua next is a function in Lua programming used to Traverse all the fields in a Table. Lua is an open-source programming language on top of the C language. It has its values across multiple platforms in both small mobile applications and large server systems. Lua functions are a group of statements that together perform a task, but the user can still divide up the code into separate functions. In Lua next, the user passes two arguments among which one is a table and the second argument is an index. Here, let us see the syntax of next in Lua and how the next function works in Lua programming.
Syntax:
Here is the syntax of the next function in Lua which helps to iterate over a table.
next(table, [, index])
Parameters:
- Table: It is the first argument in next() function
- Index: It is the second argument in next() function
How does next() Function Work in Lua Programming?
Luanext() function returns the next index of the said table and its value associated with the table.
- When the next() function is called with nil value as the second argument, next returns an initial index and the associated value.
- When the next() function is called with the last index of the table or with nil in the empty table, Lua next will return nil.
- If the second argument ‘index’ is not present, then next() functions interpret it to be nil.
- User can use next(t) to check if the table is empty.
- The order in which the indices are enumerated is not that specific, also for numeric indices.
- For traversing table in numeric order, user can use numeric for or ipairs function.
- If next() function gives undefined, during traversing, the user can assign any value to any nonexistent fields of the table.
- User can clear out or modify the existing fields in the table.
Examples of Lua Next
Following are the examples are given below:
Example #1
Simple Lua next() Function
Code:
sample_table = { "Kumar", "Keerthi", "Sameer", "Sohail", "Hardik", "Yugendar" }
for k, v in next, sample_table do
print ("Here are the values of the table ", k, v)
end
Output:
So here we have created a sample table of some values. To display these values, we are using for loop. k represents the index of the table values and v represents the actual value from the table.
For loops traverses the entries from sample_table. It returns the next index and the value pair. Let us see how the next() function gets sample_table as the parameter, also how sample_table is a valid parameter for for loop condition, also why nil is accepted as a valid step?
A more descriptive way of naming the values included in the generic loop is as follows,
For k, v1, v2, v3, …. in step, state, k0 do ….. end
And the loop goes on,
k, v1, v2, v3, …. = step(state, k0) ; if k == nil then break end; k0=k;
k, v1, v2, v3, …. = step(state, k1) ; if k == nil then break end; k1=k;
k, v1, v2, v3, …. = step(state, k2) ; if k == nil then break end; k2=k;
k, v1, v2, v3, …. = step(state, k3) ; if k == nil then break end; k3=k;
Here, the step is free to modify the state in the way the user likes to ( through pairs/ next function ). Also, let us see an example with pairs() with Lua next(),
Example #2
Luanext() Function Numbers Table
Code:
number_table = { 101, 102, 103, 104, 105, 106, 107, 108, 109 }
for index, value in pairs (number_table) do
print ("Here are the roll ID ", index, value)
end
Output:
Here, we have numbers in the table and printing the index and values. The next function can also be used with pairs() to print the next values of the table.
for k, v in pairs(table_name) do … end,
The above line basically expands to
for k, v in next, table_name, nil, do … end
Example #3
Luanext() function to check if the table is empty or not
Code:
employee = {}
if next (employee) == nil then
-- table employee is empty
print('Employee table is empty')
end -- if empty
Output:
To check if the employee table is empty or not, we can use next(table_name) to check if it is equal to nil value i.e. empty table. Here, we have declared the employee table to be empty, hence the if condition returns true.
Example #4
Luanext() Function with Null Values in the Table
Code:
employee_name = { "Saideep", "", "Karthik", "", "Chaitanya", "" }
for k, v in next, employee_name do
print ("Employees with their Role IDs", k, v)
end
Output:
So here there are some null values inside the employee table, printing null values on the console will not affect.
The original statement translates to an infinite while loop, which keeps on calling next() function with initial parameters as next(table_name, nil), and in each of the iteration, the second parameter gets replaced by the next index in the table. When next(table_name, index_n) returns nil value, loops stops or breaks. This is one of the way to traverse the table as next() can be replaced with any function giving total control over the iterations.
Conclusion
With this, we conclude the topic ‘Lua next’. We have seen what Lua next means and have seen its syntax and the required parameters for the next() function. We have also seen some of the examples and explained each of the examples in detail.Next() function along with pairs() has also been implemented here.
Recommended Articles
We hope that this EDUCBA information on “Lua Next” was beneficial to you. You can view EDUCBA’s recommended articles for more information.