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

Save a string as a table?

Started by Tjakka5, 18 July 2013 - 05:32 AM
Tjakka5 #1
Posted 18 July 2013 - 07:32 AM
Hey guys,


I'm having a little problem, and I wont go in the details.. but basically…


I have, for example, local string1 = "1234567890"
and a local table1 = {}

I then want to split, and save the string in the table, so the table becomes like this:

local table = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
So I can all the parts of the string seperatly.


Is there a way to do this?
And if yes, how?
Apfeldstrudel #2
Posted 18 July 2013 - 07:35 AM
Yah, you should look into serialize/unserialize :D/>
Tjakka5 #3
Posted 18 July 2013 - 07:37 AM
I tought it had to do something with that, but when I tried something very basic with that…
Well, let your signature speak for me.
Apfeldstrudel #4
Posted 18 July 2013 - 07:42 AM
:D/> Error? What error?
Tjakka5 #5
Posted 18 July 2013 - 07:44 AM
Prog:11: Attempt to concatenate String and Nil.


local colors = {"colors.white", "colors.orange", "colors.magenta", "colors.lightBlue", "colors.yellow", "colors.lime", "colors.pink", "colors.gray", "colors.lightGray", "colors.cyan", "colors.purple", "colors.blue", "colors.brown", "colors.green", "colors.red", "colors.black"}	
function printAppPic(x, y, file)
  file = fs.open(file, "r")
  imgLoad = file.readAll()
  if (imgLoad ~= nil and loadString ~= "") then
	imgLoadFull = {}
	imgLoadFull = textutils.unserialize(imgLoad)
  
	for i = 1, #imgLoadFull do
	  print("" ..imgLoadFull[i])
	end
  end
  file.close()
end
printAppPic(1, 1, "pic1")

The pic1 would be something like

13644291
16842974
41784614
74907414
80841456
Engineer #6
Posted 18 July 2013 - 07:45 AM
textutils wont help here. Just do this:


local str = "123456789"
local t = {}

for i = 1, str:len() do
  table.insert( t, str:sub( i, i ) )
end

This will loop through your whole string and put it in a table.
Apfeldstrudel #7
Posted 18 July 2013 - 07:49 AM
Edited out
Tjakka5 #8
Posted 18 July 2013 - 07:50 AM
I think it works, thanks :)/>

And yes, that's the full program.