Jump to content




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


  • You cannot reply to this topic
5 replies to this topic

#1 Kryptanyte

  • Members
  • 95 posts
  • LocationNew Zealand

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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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


#3 Kryptanyte

  • Members
  • 95 posts
  • LocationNew Zealand

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

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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...

#5 Kryptanyte

  • Members
  • 95 posts
  • LocationNew Zealand

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

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users