Jump to content




[SOLVED]Help with Table to String


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

#1 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 18 April 2014 - 07:02 AM

table = {"A", "B", "C")
Is there any way I can convert this to
tString = "ABC"
Without knowing how many variables I have stored?

Question was answered here : http://stackoverflow...bles-in-a-table

Edited by KingofGamesYami, 18 April 2014 - 07:08 AM.


#2 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 18 April 2014 - 07:08 AM

You want to use table.concat() for that. for more info about that function see here.

Edited by wojbie, 18 April 2014 - 07:08 AM.


#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 18 April 2014 - 07:09 AM

View Postwojbie, on 18 April 2014 - 07:08 AM, said:

You want to use table.concat() for that. for more info about that function see here.
I ninja'd you while editing my main post... LOL

#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 18 April 2014 - 07:09 AM

EDIT: Ninja'd

Yes. You would use #table to find the number of variables stored. For example:
for i = 1,#table do

Then you would use the double dot/period ( .. ) to concatenate each one onto the other like so
newString = newString .. table[i]

If you need a more explicit example let me know. One other thing - it's not a good idea to name your table "table" - especially if you don't localize the variable name 'table'

Edited by Dog, 18 April 2014 - 07:09 AM.


#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 April 2014 - 07:11 AM

do note however that table.concat only works on indexed tables, ergo any values stored under a key will not appear in the resultant string.

#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 18 April 2014 - 07:34 AM

View PostDog, on 18 April 2014 - 07:09 AM, said:

EDIT: Ninja'd

Yes. You would use #table to find the number of variables stored. For example:
for i = 1,#table do

Then you would use the double dot/period ( .. ) to concatenate each one onto the other like so
newString = newString .. table[i]

If you need a more explicit example let me know. One other thing - it's not a good idea to name your table "table" - especially if you don't localize the variable name 'table'
Yeah, I know. That was an example. My actual code is long and mostly irrelevant (making a virtual keyboard on screen :P)

View Posttheoriginalbit, on 18 April 2014 - 07:11 AM, said:

do note however that table.concat only works on indexed tables, ergo any values stored under a key will not appear in the resultant string.
I assumed it would... How would it know which comes first otherwise? Not a problem in this case.

#7 apemanzilla

  • Members
  • 1,421 posts

Posted 18 April 2014 - 12:06 PM

If for some reason you have non-number indices, you'd need to loop through the table with the "pairs" function:
local tbl = {}
tbl[1] = "a"
tbl["a"] = "b"
local str = ""
for _,v in pairs(tbl) do
  str = str..v
end
Please note however the order may not be preserved when this method is used.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users