Concatenation using Loops

Alright, so I’m attempting to create a crafting system.

For the menu I need to list all of the materials that are used in such, I was wondering if there was a more “Efficient” way of coding the following code.

[lua]
draw.SimpleText(“Materials: “…v.Mat1R…” “…v.Mat1…” “…v.Mat2R…” “…v.Mat2…””,“TabLarge”,10,20,Color(200,200,200,255))
–Notice how I have to list Mat1,2, ect all by specifically using “Concatenation”, Is there a way to do this but with a loop like the following
[/lua]

[lua]
–Table Example
–Crafting[“Pistol”] = {Name = “Pistol”, Mat1 = “Wood”, Mat1R = 100, Mat2 = “Metal”, Mat2R = 1, Mats = 2}

–X = Any number that’s lower or equal to v.Mats,
draw.SimpleText(“Materials: “…v.Mat”…X…”…"",“TabLarge”,10,20,Color(200,200,200,255))
[/lua]

[lua]
s = “Materials: %s %s %s %s”

draw.SimpleText( string.format( s, v.Mat1R, v.Mat1, v.Mat2R, v.Mat2 ), … )
[/lua]

Alright, thanks.

In general concatenating in a loop is a slow process. You’re better off doing something like this:
[lua]local strings = {}
for k, v in pairs(sometable) do
– do something to fill the table with strings
end
local concatenated = table.concat(strings, “”)[/lua]
This is more efficient because when you concatenate strings it has to reallocate memory for a string the size of the both of them. For a list of N strings it’d have to reallocate memory N-1 times, as the concatenated string gets larger more memory will be required and more time required to reallocate for it.
With table.concat it can work out how much memory it needs for all the strings together and just fill it in then, only taking up an amount of time linearly proportional to the total length of the strings, while repeated concatenation will take #S1N + #S2(N-1) + #S3*(N-2) + … + #SN.