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

Problem with textutils.unserialize

Started by BrolofTheViking, 14 October 2012 - 05:30 PM
BrolofTheViking #1
Posted 14 October 2012 - 07:30 PM

h = fs.open("blueprint","r")
blueprints = textutils.unserialize(h.readAll())
print(type(blueprints))
This is telling me that blueprints is a string, while unserialize is supposed to take a string and return it as a table I thought.
Yes, the file blueprint was written using the textutils.serialize( t ) function.
Doyle3694 #2
Posted 14 October 2012 - 07:33 PM
isn't h.readAll a table?
BrolofTheViking #3
Posted 14 October 2012 - 07:37 PM
no, readAll() returns a string, but just to be sure I tested it with readLine(). It still didn't work.
Doyle3694 #4
Posted 14 October 2012 - 07:39 PM
readAll cna't be returning a string, then my whole password program wouldn't be working but it does. Is this the whole code? and can you type the exact error?
BrolofTheViking #5
Posted 14 October 2012 - 07:40 PM
That is the entirety of the code, there is no error other than that it says that the type, after using unserialize, is a string.
http://computercraft...p?title=Fs.open according to this, readAll does return a string.
Furthermore, after testing within computercraft, readAll() does return type string.
Doyle3694 #6
Posted 14 October 2012 - 07:42 PM
is it that hard to give me the error code? Say EXACTLY what it says
BrolofTheViking #7
Posted 14 October 2012 - 07:45 PM
There is no error.
When I run that program, it prints "string"
That is the entirety of what happens.
However, I expected it to print "table"
That is the problem I am having.
I am wondering why I am not getting a table out of textutils.unserialize( s )
Lyqyd #8
Posted 14 October 2012 - 07:53 PM
Is this the whole code? It's difficult to help if you're giving only a part of the code, especially if you typed in the snippet of code rather than copying/pasting.
BrolofTheViking #9
Posted 14 October 2012 - 07:58 PM
That is the entirety of the code, there is no error other than that it says that the type, after using unserialize, is a string.
http://computercraft...p?title=Fs.open according to this, readAll does return a string.
Furthermore, after testing within computercraft, readAll() does return type string.
That is the entire program.
I have never used unserialize before, and I've barely used fs.open, so I decided to write the program in small stages, and check each stage as I went, so that if I ran into any problems I could fix them immediately, without having to search for them in a big program.
Unfortunately, I ran into a problem with the very first thing I was doing, although that is the only part of it that I've never really done before, so I had made sure to check it.

It is not a snippet of the code.
MysticT #10
Posted 14 October 2012 - 08:09 PM
Well, let us see the file you're trying to read. If you didn't save a table to the file using textutils.serialize, unserialize won't return a table.
PixelToast #11
Posted 14 October 2012 - 08:11 PM
i dont see any problems, what are the contents of the file blueprints
EDIT: :)/>/>
BrolofTheViking #12
Posted 14 October 2012 - 08:13 PM
as I mentioned earlier, what was written to the file was created with textutils.serialize.
This is the file though http://pastebin.com/hAGBnLyw
it is a list of all the block ids, and the metadata of those blocks, at whichever coordinates, written also as serialized tables within the first table, which is then serialized and written to the file.
EDIT: The file does appear to be a little bit messed up, I will try again with a smaller block list and see if I can work it out.
EDIT 2: Well, it did work when I did it with a smaller area. I guess there must be a limit to how long of a line it can write.
Lyqyd #13
Posted 14 October 2012 - 08:36 PM
Yep, that file is hosed. Ending like: "{[1]=- won't let it serialize properly.
BrolofTheViking #14
Posted 14 October 2012 - 08:40 PM
I was a little bit stupid, and assumed that unserialize would give me an error if the file was bad, rather then just return the original string.