This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kryptanyte's profile picture

[Question][String?/Numbers]Combining strings/numbers from a table to a single variable

Started by Kryptanyte, 11 March 2013 - 10:15 PM
Kryptanyte #1
Posted 11 March 2013 - 11:15 PM
As the title says, I am trying to take random letters and numbers from a table which is randomly generated, and make them all into a single variable.

I have been going through the http://lua-users.org/wiki/ for about an hour now looking for such a function, and this is my question, does this exist? or do we have to come up with some really smart way of combining them?

*EDIT*

Found it. Just by chance, table.concat(table, separator)

so if you did something like



tableToJoin = {1,2,3,4,5,6,7}

joined = table.concat(tableToJoin, ",")

It would create it exactly how it is in the table, but if you were to change the separator to like "l" then there would be a l between each table item in the variable, so like 1l2l3l4l5l6l7
theoriginalbit #2
Posted 11 March 2013 - 11:23 PM
tostring should do what you need if you are dealing with elements individually, otherwise this works if you just want the whole table in the variable

t = {1,2,3,4,"5 6 7 8 9"}
print(table.concat(t, ', '))
that will output
1, 2, 3, 4, 5 6 7 8 9
Kryptanyte #3
Posted 11 March 2013 - 11:32 PM
Yeah, I got it probably as you posted that, Its intended as a randomly generated cipher for an encryption function
theoriginalbit #4
Posted 11 March 2013 - 11:34 PM
fair enough. just on a side note this code

t = {1,2,3,4,"5 6 7 8 9", someKey = 5, ['anotherKey'] = 'aValue'}
print(table.concat(t, ' '))
will still only output

1, 2, 3, 4, 5 6 7 8 9
it doesn't do key/values…
Kryptanyte #5
Posted 11 March 2013 - 11:41 PM
Thats sort of implied, because of how the tables work with that, it doesn't print the table key if you just print() it
theoriginalbit #6
Posted 11 March 2013 - 11:43 PM
just making sure you realised, not everyone does, and not everyone knows how it works in the backend. so it could be helpful to anyone else reading the thread.