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

Http.get tables creation.

Started by Nemesison, 02 March 2013 - 08:38 PM
Nemesison #1
Posted 02 March 2013 - 09:38 PM
Title: Http.get tables creation.

Hi, i'm kinda new to the tables and http api's (even if i figured some of them out), currently i'm trying to make my program download from pastebin a series of number (example: { 1=3, 2=3, 15=2 } etc…) and put them into a dictionary, but for some reason when i try to print out the contenent of a single key (example: print( tablename[2]) the program print a blank line.

Any known solutions?

Thanks in advance.
Bubba #2
Posted 03 March 2013 - 06:25 AM
If we don't have code, we can't really help you. Are you using textutils.serialize/textutils.unserialize in order to convert the table into a useable form?
Nemesison #3
Posted 04 March 2013 - 12:00 AM
I'll explain what i need to do:

In a pastebin page i stored some data that i need for the functioning of a program, when the program runs it needs to download it from the page using the Http.get API and put it into a table that i can then use for doing stuff.

For example:

if the page contains theese numbers: 1=3, 2=3, 3=2, etc… when i download them the table that it fill will need to contains these data: tablename = { 1=3, 2=3, 3=2, etc…} in a Lua Dictionary fashion, so if i for example request the N°1 data it returns me the number 3, if i request the number 2 it return me 3, if i request 3 it returns me 2 and so on.
Bubba #4
Posted 04 March 2013 - 02:38 AM
You should definitely use textutils.serialize and textutils.unserialize to do this. Here's an example:


local t = {"table", "with", "stuff", 1, 2, ["item"] = true}
local a = textutils.serialize(t)
print(a)
This will output:
{[1]="table",[2]="with",[3]="stuff",[4]=1,[5]=2,["item"]=true,}
In string form. meaning that you could easily upload it to pastebin.

To return it to a table form, all you have to do is run textutils.unserialize on it and store the result in a variable.

Here's the wiki page for http: http://www.computercraft.info/wiki/Http.get

If you need me to walk you through either one of these I'd be willing to, but give it a shot first yourself.
Nemesison #5
Posted 04 March 2013 - 03:39 AM
Thanks i'll get right on to it!
Nemesison #6
Posted 04 March 2013 - 06:56 AM
I've done a couple of test on it but it dosen't seems to be working.

i tryied this code:


sdata = http.get("http://pastebin.com/raw.php?i=qL354k08")
usdata = textutils.unserialize(sdata)
for k,v in pairs(usdata) do print(k,v) end

but it returns me an error saying: textutilis:177 attempt to concatenete string and table
Lyqyd #7
Posted 04 March 2013 - 07:07 AM
textutils.unserialize(sdata.readAll())