Use pair:
Copy the codeThe code is as follows:
function print_contents(params)
for k, v in pairs(params) do
print(k, " ", v)
end
end
print_contents({20, 40, 50})
Use inpari:
Copy the codeThe code is as follows:
local tt =
{
[1] = "test3",
[4] = "test4",
[5] = "test5"
}
for i,v in ipairs(tt) do -- Output "test3" disconnected when k=2, because the following table of the array is not continuous, i starts from Table 1 below, and it happens to have three elements, resulting in the output when i = 3
print( tt[i] )
end
The following is the output of all the ipair when the array is continuous:
Copy the codeThe code is as follows:
function print_inpaircontents(params)
for k, v in ipairs(params) do
print(v)
end
end
local temp_table = {
[1] = "test3",
[2] = "test4",
[3] = "test5"
}
print_inpaircontents(temp_table)
pairs() can traverse the entire table, that is, include arrays and non-array parts.
The ipairs() function is used to traverse the array part in the table.