SOLVED: Odd lua and Corona behaviour

I wasn’t quite sure where to put this, but i have a question (And a massive brick wall which is holding up my project):

When i run this code:

  
json = require("json")  
keyCode = {}  
test = {}  
tables = {}  
keys = {}  
fileContents = {}  
i = 1  
p = 1  
JSONFile = system.pathForFile("QuestionnaireJSON.json")  
JSONOpen = io.open(JSONFile, "r")  
JSONContents = JSONOpen:read("\*a")  
test = json.decode(JSONContents)  
  
answerType = {}  
answerType[1] = "edit"  
answerType[2] = "memo"  
answerType[3] = "check"  
answerType[4] = "radio"  
answerType[5] = "conbo"  
  
function arrayMapping()  
for key, value in pairs(test) do  
 if type(value) == 'table' then  
 print(key, value)  
 tables[i] = value  
 tables[i].key = key  
 tables[i].value = value  
 end  
i = i +1  
end  
end  
arrayMapping()  
  

The program runs perfectly fine and i get this output:

records table: 0x186ef1c0
errors table: 0x19d820d0

However when I run this:

  
json = require("json")  
keyCode = {}  
test = {}  
tables = {}  
keys = {}  
fileContents = {}  
i = 1  
p = 1  
JSONFile = system.pathForFile("QuestionnaireJSON.json")  
JSONOpen = io.open(JSONFile, "r")  
JSONContents = JSONOpen:read("\*a")  
test = json.decode(JSONContents)  
  
answerType = {}  
answerType[1] = "edit"  
answerType[2] = "memo"  
answerType[3] = "check"  
answerType[4] = "radio"  
answerType[5] = "conbo"  
  
function arrayMapping()  
for key, value in pairs(test) do  
 print(key, value)  
 tables[i] = value  
 tables[i].key = key  
 tables[i].value = value  
i = i +1  
end  
end  
arrayMapping()  
  

I get this error:

records table: 0x18695480
totalRecords 1
Runtime error
/Users/matthewharrop/Documents/Ideagen/main.lua:25: attempt to index field ‘?’ (a number value)
stack traceback:
[C]: ?
/Users/matthewharrop/Documents/Ideagen/main.lua:25: in function ‘arrayMapping’
/Users/matthewharrop/Documents/Ideagen/main.lua:30: in main chunk
Runtime error: /Users/matthewharrop/Documents/Ideagen/main.lua:25: attempt to index field ‘?’ (a number value)
stack traceback:
[C]: ?
/Users/matthewharrop/Documents/Ideagen/main.lua:25: in function ‘arrayMapping’
/Users/matthewharrop/Documents/Ideagen/main.lua:30: in main chunk

Does anybody know why this is happening?
Please help cause i’m so very very stuck :frowning: [import]uid: 65237 topic_id: 31132 reply_id: 331132[/import]

Are you looking for the difference in the codes above?

If so, what I see is this:

[lua]function arrayMapping()
for key, value in pairs(test) do
if type(value) == ‘table’ then --< print(key, value)
tables[i] = value
tables[i].key = key
tables[i].value = value
end --<i = i +1
end
end[/lua]
Rodrigo. [import]uid: 89165 topic_id: 31132 reply_id: 124509[/import]

No, i know that (I wrote it)
The difference i am looking to understand is why one works and why one doesn’t.

BTW: Thanks for your interest! - any help is useful :slight_smile: [import]uid: 65237 topic_id: 31132 reply_id: 124511[/import]

Excuse me. I`ve really misunderstood what you meant.

BTW, I think I am unable to help you further because I do not know as well (at least by now), sorry.
PS: Surely some dev will get it for you.

[import]uid: 89165 topic_id: 31132 reply_id: 124512[/import]

Haha, No worries! Its probably written badly (I did it in a moment of pure frustration) [import]uid: 65237 topic_id: 31132 reply_id: 124513[/import]

I suspect the problem is the line [lua]tables[i].value = value[/lua], as you can’t use the dot notation in lua for numerical keys.

For example, this code is not allowed:
[blockcode]
local numberKey = 1
local table = {}

table.numberKey = “Hello!” – This line is not allowed, because numberKey is a number
[/blockcode]

In your case, [lua]value[/lua] is the integer 1 (as we can see from the output of the print statement just before the error message), so the expression [/lua]tables[i].value[/lua] will generate an error. This is consistent with the error message, which says you’re trying to index (i.e., use the dot notation) using a number value.

Hope this helps.

  • Andrew
    [import]uid: 109711 topic_id: 31132 reply_id: 124519[/import]

Why is it like that? Surely the notation shouldn’t effect the data you’re trying to save?
Thanks for the help :slight_smile: [import]uid: 65237 topic_id: 31132 reply_id: 124524[/import]

Furthermore, I tested it with the example on the site and that worked fine. It just seems that when i remove the if type(...) elseif end statement then it fails… [import]uid: 65237 topic_id: 31132 reply_id: 124542[/import]

Yes, the reason it works when you have that statement, and fails when you remove it, is because that statement effectively prevents indexing [lua].value[/lua] when value is a number.

Not sure I understand your first question though. The notation doesn’t affect or change the data you’re trying to save. However, to use the dot notation, the value of the variable after the dot cannot be (or start with) a number. This is a lua syntax requirement. You can only index into a table with a number by using the bracket notation.

  • Andrew [import]uid: 109711 topic_id: 31132 reply_id: 124564[/import]

Sorry, you must’ve misunderstood me. When i ran the example in the corona documentation with a few additions i get this:

  
local people = {  
 { name="Bob", age=32, gender="male" },  
 { name="Jane", age=29, gender="female" }  
 }  
  
 print( people[1].name ) -- output: Bob  
 print (people[1].age)  
 people[1].age = 46  
 print (people[1].age)  
 print( people[2]["gender"] ) -- output: female  
 print (people[2].age)  
 people[2].age = "thirty-four"  
 print (people[2].age)  
  

Output:

Bob
32
46
female
29
thirty-four

So why, in this example, can i have a dot notation which is a number but not in my own? [import]uid: 65237 topic_id: 31132 reply_id: 124679[/import]

Ah, I see what you mean. Unfortunately I’m away traveling right now without my computer, so I can’t test what I’m about to suggest, but I’d try the following.

The problem may be that [lua]tables[i][/lua] is not pre-declared as a table. Before your print statement, try putting [lua]tables[i] = {}[/lua] and see of that helps.

  • Andrew [import]uid: 109711 topic_id: 31132 reply_id: 124683[/import]

That works! Thats great, you’ve put me right back on track! Thanks so much for your help! :smiley: [import]uid: 65237 topic_id: 31132 reply_id: 124691[/import]

Are you looking for the difference in the codes above?

If so, what I see is this:

[lua]function arrayMapping()
for key, value in pairs(test) do
if type(value) == ‘table’ then --< print(key, value)
tables[i] = value
tables[i].key = key
tables[i].value = value
end --<i = i +1
end
end[/lua]
Rodrigo. [import]uid: 89165 topic_id: 31132 reply_id: 124509[/import]

No, i know that (I wrote it)
The difference i am looking to understand is why one works and why one doesn’t.

BTW: Thanks for your interest! - any help is useful :slight_smile: [import]uid: 65237 topic_id: 31132 reply_id: 124511[/import]

Excuse me. I`ve really misunderstood what you meant.

BTW, I think I am unable to help you further because I do not know as well (at least by now), sorry.
PS: Surely some dev will get it for you.

[import]uid: 89165 topic_id: 31132 reply_id: 124512[/import]

Haha, No worries! Its probably written badly (I did it in a moment of pure frustration) [import]uid: 65237 topic_id: 31132 reply_id: 124513[/import]

I suspect the problem is the line [lua]tables[i].value = value[/lua], as you can’t use the dot notation in lua for numerical keys.

For example, this code is not allowed:
[blockcode]
local numberKey = 1
local table = {}

table.numberKey = “Hello!” – This line is not allowed, because numberKey is a number
[/blockcode]

In your case, [lua]value[/lua] is the integer 1 (as we can see from the output of the print statement just before the error message), so the expression [/lua]tables[i].value[/lua] will generate an error. This is consistent with the error message, which says you’re trying to index (i.e., use the dot notation) using a number value.

Hope this helps.

  • Andrew
    [import]uid: 109711 topic_id: 31132 reply_id: 124519[/import]

Why is it like that? Surely the notation shouldn’t effect the data you’re trying to save?
Thanks for the help :slight_smile: [import]uid: 65237 topic_id: 31132 reply_id: 124524[/import]

Furthermore, I tested it with the example on the site and that worked fine. It just seems that when i remove the if type(...) elseif end statement then it fails… [import]uid: 65237 topic_id: 31132 reply_id: 124542[/import]

Yes, the reason it works when you have that statement, and fails when you remove it, is because that statement effectively prevents indexing [lua].value[/lua] when value is a number.

Not sure I understand your first question though. The notation doesn’t affect or change the data you’re trying to save. However, to use the dot notation, the value of the variable after the dot cannot be (or start with) a number. This is a lua syntax requirement. You can only index into a table with a number by using the bracket notation.

  • Andrew [import]uid: 109711 topic_id: 31132 reply_id: 124564[/import]