lua table concanation
-- Define two tables
table1 = {1, 2, 3}
table2 = {4, 5, 6}
-- Concatenate table2 to table1
for _, v in ipairs(table2) do
table.insert(table1, v)
end
-- Display the concatenated table
for _, v in ipairs(table1) do
print(v)
end
Explanation:
- Two tables,
table1
andtable2
, are defined with elements{1, 2, 3}
and{4, 5, 6}
respectively. - A
for
loop is used to iterate through each value (v
) intable2
. - Inside the loop,
table.insert()
function is used to insert each value fromtable2
(v
) intotable1
. - Finally, another
for
loop is used to iterate throughtable1
and print each element, which now includes the concatenated elements fromtable2
.