Updated April 19, 2023
Introduction to Lua iterate table
The Lua iterate table is one of the standard and most important library of the lua script by using these we can iterate the table values using key-value pairs and also when iterating the value pairs that time there is no specified order for traversal in both up and down traversal directions even if the keys of the table are also in numeric format the keys must contain numeric and unique one and the values are always iterated whenever we construct the elements the traverse of the elements are also called with the either collection or containers of the script.
Syntax:
In lua script iterate table is used for to construct and traverse the elements which is on the key-value pairs method. The script collections often used for to refer the tables and most often used to various data structures including arrays, strings etc.
Any loop(for, while) key, value in pairs(table input) do
-----some lua script codes depends upon the user requirement---
end
The above codes are the basic syntax for utilising the lua script tables by using the loops we can iterate the table values like key, value pairs. For these pairs the key should be unique also numeric one and the value may be change depends upon the requirement.
How to iterate the table in Lua?
The Lua standard library have many defaults methods, packages and the loop conditional statements. By the way in lua tables are one of the feature and it is mainly used for storing the datas in table format like rows, columns formats. Even the users are to construct the table using some expressions like {} open and close parentheses for to construct the empty tables on the script. So it is mainly known that there is no fixed and constant mode with relationship between the variables as mentioned by the user it’s to be varied upon the requirement. And also whenever we used iterator function the table values uses generic loops also the iterator function repeatedly called and returns the key and value pairs for each table items as mentioned by the user input. For each table item the values are starting at the integer 1 and until it finds the other integer keys that is missing one if the key value is 0 to 8 are present and the key value of next integer like 9 is nil it will always return the first set of 8 integer values this will also occurs at even if the key value of 10 is present or not-present. In other cases we cannot use the ipairs for to iterate over the tables with some holes on the every key ranges.
Examples of Lua iterate table
Here are the following examples mention below
Example #1
Code:
function vars()
print [[
"Thank you for choosing the online card game using lua script"
]]
end
function vars()
initialbal = 300
math.randomseed( os.time() )
end
function vars1(eg8)
local eg={"jack", "king", "queen", "ace",1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
return eg[eg8]
end
function vars2()
print("Thank you for choosing our game please play well ")
print()
end
function vars3( amt )
initialbal = initialbal + amt
vars2()
end
function vars4()
local nxtcrd = math.random(3,9)
return nxtcrd
end
function vars5()
print("Thank you for chooisng game your game is in-progress:")
local crdval = {eg1=vars4(),eg2=vars4()}
print( "Your first game is started:" .. vars1(crdval.eg1) )
print( "Your second game is started:" .. vars1(crdval.eg2) )
return crdval
end
function vars6()
eg3 = false
local btamt = 10
repeat
io.write("Please place your amount:");
local amt = io.read();
btamt = tonumber(amt)
if btamt==0 then
print("Thanks user your amount is placed on the table and please be continue!")
print()
eg3 = true
end
if btamt <= initialbal then
eg3 = true
else
print("Please place correct amount")
print("Thank you for placing the amount.")
end
until eg3
return btamt
end
function vars7( crdval )
local ply1 = vars4()
local crdfirst = crdval.eg1
local crdsecond = crdval.eg2
print( "Please put your dealer card"..vars1(ply1) )
local eg5 = math.min( crdfirst, crdsecond )
local eg6 = math.max( crdfirst, crdsecond )
if ply1>=eg5 and ply1 <=eg6 then
print("Thank you user you won the battle")
return true
end
for eg5,eg6 in pairs(eg) do
print(eg5,eg6)
end
print("Sorry user you have lost the battle please try again")
return false
end
function vars8()
print[[
"please try your game again"
]]
rsp = io.read()
if rsps == "Y" then
return true
else
print("Thank you users")
return false
end
end
vars()
vars8()
repeat
local crdval = vars5()
local btamt = vars6()
if btamt == 0 then
eg7=true
else
if vars7(crdval) then
vars3( btamt )
else
vars3( -btamt )
if initialbal <= 0 then
if vars8()==true then
vars8()
end
eg7=true
end
end
end
until eg7
Output:
In the above example, we have created simple card games by using some logic. We can implement the logic with some sample inputs the user input is validated and iterated using the for loops. The card values are iterated and the players and cards are denoted and marked as the key and value pairs.
Example #2
Code:
print ("Welcome My Domain have a nice day")
print ("Its a second example")
vars = {"first","second","third","four","five"}
for i=1,#vars do
print (i,vars[i])
end
print ("Please provide your first input")
vars[8] = "four"
for i=1,#vars do
print (i,vars[i])
end
print ("Please provide your second input")
vars["second"] = "is a second value"
for i=1,#vars do
print (i,vars[i])
end
print ("We can compare the values")
for i,v in pairs(vars) do
print (i,v)
end
print ("Your final outputs and results")
for i,v in ipairs(vars) do
print (i,v)
end
Output:
In second example we used stateless iterator concept. We can compare the variable values and using for loop to iterate the values for each table item we can pair the values by using the table indexes. When we specify the index it denotes and declare the values the rest of the indexes declared as nil values.
Example #3
Code:
vars = {"Example1", "Example2", "Example3"}
for i, v in ipairs(vars) do
print(i, v)
end
function demo (vars, i)
i = i + 1
local v = vars[i]
if v then
return i, v
end
end
function demo1 (vars)
return demo, vars
end
function demo2(vars1)
return demo1
end
Output:
In the final example, we used three functions for calling the variable vars. We have assigned the values for the variable and by using the for loop we can iterate the values also here we used key and value pairs we used stateless iterator logic.
Conclusion
In the Lua script, the iterator is one of the concepts for iterating the user inputs. The inputs are marked as the key, value pairs so the keys are numeric and it’s a unique one the values are different and dynamically changed on the script depends upon the requirement.
Recommended Articles
We hope that this EDUCBA information on “Lua iterate table” was beneficial to you. You can view EDUCBA’s recommended articles for more information.