Jump to content




String to table


  • You cannot reply to this topic
16 replies to this topic

#1 mibac138

  • Members
  • 132 posts
  • LocationPoland

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 by mibac138, 03 February 2014 - 01:43 PM.


#2 ingie

  • Members
  • 69 posts
  • LocationMannin

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 by ingie, 03 February 2014 - 02:17 PM.


#3 mibac138

  • Members
  • 132 posts
  • LocationPoland

Posted 03 February 2014 - 02:17 PM

Oh, thanks! I didnt see it before :P

#4 surferpup

  • Members
  • 286 posts
  • LocationUnited States

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.

#5 mibac138

  • Members
  • 132 posts
  • LocationPoland

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 by mibac138, 04 February 2014 - 08:43 AM.


#6 ingie

  • Members
  • 69 posts
  • LocationMannin

Posted 04 February 2014 - 09:44 AM

View Postmibac138, on 03 February 2014 - 02:17 PM, said:

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 :)


View Postmibac138, on 04 February 2014 - 08:41 AM, said:

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 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 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

View Postmibac138, on 04 February 2014 - 08:41 AM, said:

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

#7 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

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...

#8 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 04 February 2014 - 04:06 PM

View Postmibac138, on 04 February 2014 - 08:41 AM, said:

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 by surferpup, 04 February 2014 - 04:12 PM.


#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#10 surferpup

  • Members
  • 286 posts
  • LocationUnited States

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 by surferpup, 04 February 2014 - 05:17 PM.


#11 mibac138

  • Members
  • 132 posts
  • LocationPoland

Posted 05 February 2014 - 12:52 PM

Do any one know how to do that string.gsub(string, "%" .. i, data[i]) will work with % .. number (ex. 1) ?

#12 CometWolf

  • Members
  • 1,283 posts

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[i]

#13 mibac138

  • Members
  • 132 posts
  • LocationPoland

Posted 05 February 2014 - 01:24 PM

Thanks!

Edited by mibac138, 05 February 2014 - 01:37 PM.


#14 mibac138

  • Members
  • 132 posts
  • LocationPoland

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 by mibac138, 05 February 2014 - 01:37 PM.


#15 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 05 February 2014 - 02:40 PM

Did you mean to write "%d1" or "%d",1 ??

#16 CometWolf

  • Members
  • 1,283 posts

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 by CometWolf, 05 February 2014 - 02:48 PM.


#17 mibac138

  • Members
  • 132 posts
  • LocationPoland

Posted 06 February 2014 - 05:23 AM

Oh, thanks! :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users