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

How do I properly use table.concat?

Asked by 7 years ago

I am making a disaster script and I want the end of the round to display the names of the survivors which are derived from the table. I want to change the table into a string and change the gui text into the string. Can somebody simply show me how to properly use table.concat to turn the table into a string?

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

The concat in table.concat is short for concatenate (if you didn't already know). Just thought I'd mention that so you know why the name of the function makes sense.

Arguments

The function has two mandatory arguments, and two optional arguments. Obviously, the first two mandatory arguments would be the table (of which elements are concatenated), and a string (the character(s) in between each concatenation). Here's an example:

local t = {"a", "b", "c", "d"}
print( table.concat(t, " , ") ) -- > "a , b , c , d"

The code above converts the given table to a string of all it's elements which are separated by the string passed as the second argument. So that leaves us with the last two (optional) arguments...

Range

The last two arguments are integers, which represent the index range of the table to include in the concatenated string. For example, let's say you wanted all the elements in your table listed, except for the first and last. This is where you could use range:

local t = {"a, "b", "c", "d"}
print( table.concat(t, " , ", 2, #t-1) ) -- > "b , c"

This is telling the function to start at the second index of the table, and list all of the elements up until the last index of the table subtracted by one (the #t-1 expression, which is the last index of the table minus one).


Hope this helped, let me know if you have any questions.

0
Thanks. I'll try using this in my script. ZetaReticuli 38 — 7y
0
Also, is there any way to view the same table from a different script or even script type? ZetaReticuli 38 — 7y
Ad

Answer this question