Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How does one use table.concat on tables?

What exactly is table.concat's function, and how does one use it?

1 answer

✓ Accepted answer

If you're familiar with concatenation, the function behaves like it sounds. It will take all of the entries in an array (it must be an array), and concatenate them together with some delimiter defined by the second (optional) argument. The result is a string. Here's an example:

local array = {1, 2, 3, 4}
print(table.concat(array)) --> 1234

When no second argument is given, there is no delimiter and the entries are not separated in the string. Here's another example, this time using the second optional argument. As noted before, this argument represents the intermittent string (delimiter) between all entries of the array.

local array = {1, 2, 3, 4}
print(table.concat(array, "; ")) --> 1; 2; 3; 4

Also notice that it will not apply the delimiter to the last entry (the 4 does not have a semicolon after it, like the rest do). This function can be very useful, and it's especially useful if you're looking for a quick-and-easy way to display the content of an array (as an alternative to unpack).

For one example of good implementation of table.concat, you can view an old post I made here that uses it to solve a problem with strings.

6 comments

0

So it turns them all into one string? 

0

Yes, that's correct.

0

I updated my answer with a little bit more information, if you're interested.

1

Would also like to note that concat errors for non string non number values

0

Good point ^

0

You also forgot the last two arguments which is where to start and where to end concatenating bit nonetheless good post, +1

This discussion is preserved as it was. New answers, comments, and votes are closed.