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

Converting a string to table

Started by Nothy, 14 December 2016 - 08:55 PM
Nothy #1
Posted 14 December 2016 - 09:55 PM
Alright, before I weird you out.

I'm sending data between machines, and I want to list the data as icons. Now, the tricky part is that I can't just slap the string as is on there.


An example of what the string would look like:
local str = "potato, cabbage, carrot"

And I'm trying to figure out the conversion from that to

local str = "potato, cabbage, carrot"
local strTable = {
  "potato",
  "cabbage",
  "carrot"
}
And in true Nothy style, I'm not quite sure how I'd go about it. So I'm asking you for help.
Edited on 14 December 2016 - 08:55 PM
Lupus590 #2
Posted 14 December 2016 - 10:14 PM
Textutils provides a pair of functions which does what you seem to be needing:
http://computercraft.info/wiki/Textutils.serialize
http://computercraft.info/wiki/Textutils.unserialize
Nothy #3
Posted 14 December 2016 - 10:19 PM
Textutils provides a pair of functions which does what you seem to be needing:
http://computercraft...utils.serialize
http://computercraft...ils.unserialize

Yes, and I figured out that I could also use this:

for word in string.gmatch("Hello Lua user", "%a+") do print(word) end
source: http://lua-users.org/wiki/StringLibraryTutorial
supernicejohn #4
Posted 14 December 2016 - 10:31 PM
Queue Kingofgamesyami coming up with a better explanation

Well, you need to separate each substring between the ",", think loops, and finding stuff, then just insert it into the table, which you'll get numerically indexed.
Of course there are several ways to do it, for these thing I like just basic find() and sub(). But that's me…

EDIT: I type slowly… ;(
Edited on 14 December 2016 - 09:33 PM
Nothy #5
Posted 15 December 2016 - 03:20 PM
Alright, thanks to you two.
It was just a matter of using textutils.serialize and textutils.unserialize