Right now I'm trying this:
tab = {
fiv = {
Q1 = "yy"
},
vis = {
Q1 = "xx"
}
}
red = {
Q1 = "zz"
}
tab[#tab+1] = red
print(tab.red.Q1)
I get the error: attempt to index field 'red' (a nil value)
Right now I'm trying this:
tab = {
fiv = {
Q1 = "yy"
},
vis = {
Q1 = "xx"
}
}
red = {
Q1 = "zz"
}
tab[#tab+1] = red
print(tab.red.Q1)
I get the error: attempt to index field 'red' (a nil value)
Well, the problem here is a common one, that being that you treated a Dictionary as if it was an Array. The difference between the two is that an array has numeric (number) keys and a dictionary has primarily string keys
--arrays
local arr1 = {1,2,3,4,5}
local arr2 = {[1] = 1, [2]=2,[3]=3}
--dictionaries
local dict = {Thing = 1,Foo = "hello"}
local dict = {["Thing"] = 1,["Foo"] = "hello"}
On line 17, you inserted table red into table tab as if tab was an array, by getting the length of it (for dictionaries it is 0) and adding one to it.
A way to fix this would be:
local tab = {
fiv = {
Q1 = "yy"
},
vis = {
Q1 = "xx"
}
}
local red = {
Q1 = "zz"
}
tab.red = red --tab["red"]=red also works
print(tab.red.Q1)
Hopefully This Helped!
This discussion is preserved as it was. New answers, comments, and votes are closed.