• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Tables (1 Viewer)

Alienhead

Newcomer
Joined
Mar 29, 2022
Messages
5
Reaction score
0
I'm trying to create a list of tables or a table label variable.
What is the format to do the following, and is it even possible.

Lua:
console = {}
console[1].data = "a string"
console.data[1] = "a string"
I need to be able to index the table by the number but I also need multiple data per indexed entry.
Lua:
console = {}
console[1].data = "a string"
console[1].data2 = "a string"
console.data[2] = "a string"
console[2].data2 = "a string"
I know both the above syntax do not work, but my question is - is there anyway or proper format to attempt to do what I've descibed above?

Thank you.
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
75
Reaction score
9
Do you know that the table is the variable "console" and not its childs?, but you can't do:
Lua:
console[1] = something
Because console[1] is not a table, in fact, is not anything, the correct would be:

Lua:
console = {}
console[1] = {}
console[1].data = "a string"
-- Or
console = {{}} -- a table stored at [1] in console
console[1].data = "a string"
-- Or
console = {{data = "a string"}}
Same case with your other example:
Lua:
console = {data = {"a string"}} -- The final result of console.data[1] = "a string"
 
Top