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

String to table

Started by mibac138, 03 February 2014 - 12:41 PM
mibac138 #1
Posted 03 February 2014 - 01:41 PM
Hey!

Im trying to get table from this site, using this.


local httpAddress = "http://www.timeapi.org/a/now?format={[%22sec%22]=%25S,%20[%22time%22]=%25H:%25M,%20[%22day%22]=%25d,%20[%22month%22]=%25b,%20[%22year%22]=%25Y}"
local websiteData = http.get(httpAddress).readAll() or "Err"

And my problem is that i tried using textutils.unserialize(websiteData), i was trying as hard as i can, but i cant do from websiteData table.
Can anyone help me?

BTW. i used a (www.timeapi.org/a/now), becouse its my local time (UTC / GMT +1)
Edited on 03 February 2014 - 12:43 PM
ingie #2
Posted 03 February 2014 - 02:14 PM
it's because of the format you've used

if you view the websiteData variable, then you'll see that it's this:

{["sec"]=10, ["time"]=20:25, ["day"]=03, ["month"]=Feb, ["year"]=2014}

note that the values have no quotes, so they'll error if decoded, therefore there is no valid table to unserialize - so it just returns the string as was.

you really want your api formatting to result in this

{sec="10", time="20:25", day="03", month="Feb", year="2014"}

…the ["bracketed quotes"] around the keynames are optional in this scenario, so i've left them out
Edited on 03 February 2014 - 01:17 PM
mibac138 #3
Posted 03 February 2014 - 02:17 PM
Oh, thanks! I didnt see it before :P/>
surferpup #4
Posted 03 February 2014 - 02:45 PM
Check out my time API Real World Time API (Ver. 1.0)– it does it for you using timeapi.org info. Plus, it will give it back to you any way you want :)/>

It took me a while to figure out the urlEncode function to make it work with timpani.org. It is tricky.

Feel free to tear into my code or just use the API.
mibac138 #5
Posted 04 February 2014 - 08:41 AM
I know about your API already, and it was my inspiration, i wanted to do something on my own :)/>
BTW. it didnt work (lol?) i did:

api = dofile("timeapi")
api.printTime([...])
Edited on 04 February 2014 - 07:43 AM
ingie #6
Posted 04 February 2014 - 09:44 AM
Oh, thanks! I didnt see it before :P/>

hehe. it's usually the simplest things
i read the output several times before i saw it staring me in the face :)/>


I know about your API already, and it was my inspiration, i wanted to do something on my own :)/>

to make it more proactive in Ask a Pro, and as [member='Lyqyd'] has detailed here in the Read This Before Asking thread, it's always better to state the full reason behind something…

lyqyd said:
When creating a question topic, be sure to fully explain your question. If you're having a problem with a piece of code, you should explain three things about it. Explain what you're trying to do, the big picture goal for the program. [..] Next, explain what you expect the piece of code that you're having trouble with to do. Finally, explain what the code is actually doing instead of what you want it to do.

that's not a complaint at all, as "your inspiration" is not explicit in that text, but i'm sure you see how it fits the same goal as lyqyd's message :)/> - your original question would have been better if it had said "I was inspired by ABC to do XYZ so as to learn it myself"


also, within your clarification, do you mean you're trying to replicate almost the same thing using your own new code?… or are you're trying to do a slightly different task than [member="surferpup"]'s API?

- both are equally good to do -i'm not knocking either reason- but if you're modifying his API, then a good question might be "Is is possible to do XYZ by changing your api in someway - don't tell me the way to do it, as I want to work that out myself"

and that way, you can hack, but still have the enjoyment of assisted self-discovery - but also with the knowledge that you have probably a flattered author of the api, who will be the best person to ask if you get stuck


i hope that makes sense…
- i'm not trying to poke fun or rules at you or anything like that. just sayin' :)/>


oh, and

BTW. it didnt work (lol?) i did:

api = dofile("timeapi")
api.printTime([...])

is about as useful as a conversation starter which says "You know what? I went to the shops yesterday…" and nothing else.
you can't say that without everyone going "AND? … What happened?" :)/>

*cough* - see the last part of that quoted section from the forum sticky *cough*

it may not be relevant to you anymore if you've decided to write your own, but it'd still be nice to know how something didn't work rather than just saying it didn't as it could be a valid bug… or it could be operator error
LBPHacker #7
Posted 04 February 2014 - 10:51 AM
Guys… Close those poor HTTP hanldes for once…
local handle = http.get("http://localhost")
print(handle.readAll())
handle.close() -- please...
Actually, I'm not sure if the handle gets garbagecollected with the handle variable, but there's a close method, so…
surferpup #8
Posted 04 February 2014 - 04:06 PM
I know about your API already, and it was my inspiration, i wanted to do something on my own :)/>
BTW. it didnt work (lol?) i did:

api = dofile("timeapi")
api.printTime([...])

there was an error in the timeout function. Fixed. Thanks for catching it.


api = os.loadAPI("timeapi")
api.printTime()


Works fine. As with any code – no feedback means errors go uncaught. If you find another error, please report it to me.
Edited on 04 February 2014 - 03:12 PM
Lyqyd #9
Posted 04 February 2014 - 04:57 PM
That code wouldn't work fine; the loadAPI call will return a boolean, which you then attempt to index.
surferpup #10
Posted 04 February 2014 - 05:01 PM
That was a big deep on my part – I was flustered a bit.

You are correct.

In lua interactive, I actually ran:


os.loadAPI("timeapi")
timeapi.printTime()

in a program (for Eastern time with no offset and a default format):


os.loadAPI("timeapi")
timeapi.printTime("est",0,timeapi.format.default)

The ellipses (…) were uncecessary.

I am unfamiliar with the dofile approach to loading an api.
Edited on 04 February 2014 - 04:17 PM
mibac138 #11
Posted 05 February 2014 - 12:52 PM
Do any one know how to do that string.gsub(string, "%" .. i, data) will work with % .. number (ex. 1) ?
CometWolf #12
Posted 05 February 2014 - 01:05 PM
That was a really derpy sentence there, but Im gonna assume you want the digit pattern which is %d.

string.gsub(string,"%d"..i,data[i])
This code will replace every instance of a number concatenated with whatever i is with what's contained in data
mibac138 #13
Posted 05 February 2014 - 01:24 PM
Thanks!
Edited on 05 February 2014 - 12:37 PM
mibac138 #14
Posted 05 February 2014 - 01:36 PM
Soooo why this doesnt work? (It should give 21 but it doesnt change)

data = {}
data[1] = 21
a = 1
b = string.gsub(a, "%d1", tostring(data[1]))
--> b
1

--> a
1
Edited on 05 February 2014 - 12:37 PM
surferpup #15
Posted 05 February 2014 - 02:40 PM
Did you mean to write "%d1" or "%d",1 ??
CometWolf #16
Posted 05 February 2014 - 02:47 PM
If you just want to replace the number one, you just use "1". "%d" will replace any number. "%d1" would be any number followed by a 1.
Edited on 05 February 2014 - 01:48 PM
mibac138 #17
Posted 06 February 2014 - 05:23 AM
Oh, thanks! :)/>