How to get data from inside the table correctly ?

Hi Corona community,

I use mod_pars by develephant, but I think my question is general and it has nothing to do with parse

this is the print statement :

[lua]local t = event.response.results
print( #t )[/lua]

and here’s what is printed in the console :

Aug 19 01:48:23.188: response: Aug 19 01:48:23.188: results: Aug 19 01:48:23.188: 1: Aug 19 01:48:23.188: createdAt: 2015-08-15T16:41:08.445Z Aug 19 01:48:23.188: objectId: 6qrED6IZ3W Aug 19 01:48:23.188: player: Aug 19 01:48:23.188: \_\_type: Pointer Aug 19 01:48:23.189: className: \_User Aug 19 01:48:23.189: objectId: g5hl75tjST Aug 19 01:48:23.189: score: 12365 Aug 19 01:48:23.189: updatedAt: 2015-08-15T16:41:08.835Z Aug 19 01:48:23.189: 1

now as you see, there’s the objectId and the score in the player section. I want to print only the score

I tried doing something like this :

[lua]print(#t[1][objectId])
–or
print(#t[1][score])[/lua]

but I get runtime error ! 

how can I print things correctly ?

thanks.

*** UPDATED TO FIX ERROR IN ANSWER ***

Try this:

print(t[1]["objectId"]) --or print(t[1]["score"]) 

 or this:

print(t[1].objectId) --or print(t[1].score)

Both are valid techniques.

To elaborate further…

local tmp = {} tmp[1] = 10 tmp.a = "Lua" tmp["b"] = "Is Cool" print(tmp[1], tmp.a, tmp.b ) print(tmp[1], tmp["a"], tmp["b"])

See how strings and named fields are interchangeable?

Now try tables in tables:

local tmp = {} tmp[1] = { age = "10", name = "Bob" } tmp[2] = { age = "12", name = "Bill" } tmp[3] = { age = "9", name = "Sue" } for i = 1, #tmp do print("User # ", i ) print("Name: ", tmp[i].name ) print("Age: ", tmp[i]["age"] ) end

Finally, this is the structure of your original table (from first post; not including references to userdata fields):

local results = { createdAt = "2015-08-15T16:41:08.445Z", objectId = "6qrED6IZ3W", player = { objectId = "g5hl75tjST" score = 12365 updatedAt = "2015-08-15T16:41:08.835Z" } }

@

roaminggamer

you did not only help fix the problem I’m facing, you teached me how to get data correctly which was the hardest thing for me  :wub:

I don’t think ‘thank you’ is enough to thank you, but thank you for this help  :rolleyes:

@roaminggamer, on more little thing, it seems like I’m getting the first objectId : “6qrED6IZ3W”

how can I print the second one which is in the player table ?! , I’m printing like this as you said :

print(t[1][“objectId”])
–or
print(t[1][“score”]) 

the score come correctly, but when it comes to the objectId, I get the first one, not the second as it meant to be. 

The player table should be (if I’m reading it right):

local player = event.response.results[1].player print( player.objectID )

Yes, that worked, thank you

*** UPDATED TO FIX ERROR IN ANSWER ***

Try this:

print(t[1]["objectId"]) --or print(t[1]["score"]) 

 or this:

print(t[1].objectId) --or print(t[1].score)

Both are valid techniques.

To elaborate further…

local tmp = {} tmp[1] = 10 tmp.a = "Lua" tmp["b"] = "Is Cool" print(tmp[1], tmp.a, tmp.b ) print(tmp[1], tmp["a"], tmp["b"])

See how strings and named fields are interchangeable?

Now try tables in tables:

local tmp = {} tmp[1] = { age = "10", name = "Bob" } tmp[2] = { age = "12", name = "Bill" } tmp[3] = { age = "9", name = "Sue" } for i = 1, #tmp do print("User # ", i ) print("Name: ", tmp[i].name ) print("Age: ", tmp[i]["age"] ) end

Finally, this is the structure of your original table (from first post; not including references to userdata fields):

local results = { createdAt = "2015-08-15T16:41:08.445Z", objectId = "6qrED6IZ3W", player = { objectId = "g5hl75tjST" score = 12365 updatedAt = "2015-08-15T16:41:08.835Z" } }

@

roaminggamer

you did not only help fix the problem I’m facing, you teached me how to get data correctly which was the hardest thing for me  :wub:

I don’t think ‘thank you’ is enough to thank you, but thank you for this help  :rolleyes:

@roaminggamer, on more little thing, it seems like I’m getting the first objectId : “6qrED6IZ3W”

how can I print the second one which is in the player table ?! , I’m printing like this as you said :

print(t[1][“objectId”])
–or
print(t[1][“score”]) 

the score come correctly, but when it comes to the objectId, I get the first one, not the second as it meant to be. 

The player table should be (if I’m reading it right):

local player = event.response.results[1].player print( player.objectID )

Yes, that worked, thank you