Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation of tables in Lua

ORIGINAL POST

Given that there is no built in function in Lua, I am in search of a function that allows me to append tables together. I have googled quite a bit and have tried every solutions I stumbled across but none seem to work properly.

The scenario goes like this: I am using Lua embeded in an application. An internal command of the application returns a list of values in the form of a table.

What I am trying to do is call that command recursively in a loop and append the returned values, again in the form of a table, to the table from previous iterations.


EDIT

For those who come across this post in the future, please note what @gimf posted. Since Tables in Lua are as much like arrays than anything else (even in a list context), there is no real correct way to append one table to another. The closest concept is merging of tables. Please see the post, "Lua - merge tables?" for help in that regard.

like image 218
John Mark Mitchell Avatar asked Sep 11 '09 13:09

John Mark Mitchell


People also ask

How do I concatenate a table in Lua?

Since Tables in Lua are as much like arrays than anything else (even in a list context), there is no real correct way to append one table to another. The closest concept is merging of tables.

What does concatenate mean in Lua?

Lua string concatenation is defined as the joining of the two strings using string operator provided by Lua programming language that is two-period operator ( . . ) which is used for joining the given strings into one string.

How do I unpack a table in Lua?

unpack() function in Lua programming. When we want to return multiple values from a table, we make use of the table. unpack() function. It takes a list and returns multiple values.

Which is the string concatenation operator in Lua?

Lua denotes the string concatenation operator by " .. " (two dots). If any of its operands is a number, Lua converts that number to a string.


1 Answers

Overcomplicated answers much?

Here is my implementation:

function TableConcat(t1,t2)     for i=1,#t2 do         t1[#t1+1] = t2[i]     end     return t1 end 
like image 69
Weeve Ferrelaine Avatar answered Sep 23 '22 15:09

Weeve Ferrelaine