Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How does one use table.concat on tables?

Asked by 5 years ago

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

1 answer

Log in to vote
4
Answered by 5 years ago
Edited 5 years ago

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.

0
So it turns them all into one string?  Stephenthefox 94 — 5y
0
Yes, that's correct. ScriptGuider 5640 — 5y
0
I updated my answer with a little bit more information, if you're interested. ScriptGuider 5640 — 5y
1
Would also like to note that concat errors for non string non number values User#24403 69 — 5y
View all comments (2 more)
0
Good point ^ ScriptGuider 5640 — 5y
0
You also forgot the last two arguments which is where to start and where to end concatenating bit nonetheless good post, +1 User#24403 69 — 5y
Ad

Answer this question