What exactly is table.concat's function, and how does one use it?
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.