Nested tables and objects in tables

A question for the community!

How often does one need to use nested tables? I understand that it is possible and have seen the syntax, but in general one should be able to put a project together with that added complexity… Right? If there are any practical examples where it makes more sense to use a nested table than not please share.

Also, in most of the sample code I have seen people seem to avoid putting objects into tables and instead will use text to refer to a file name or something. I’m specifically talking about situations where one needs to store images or sound files in a table. Again, any examples would be appreciated!

Just getting started with corona but seems promising so far! [import]uid: 191855 topic_id: 33277 reply_id: 333277[/import]

Hmmm… well, the “short answer” is, the fewer up-referenced tables and variables you have in memory, the better. In fact, Lua will choke if you have too many of them in one module. Nesting is a perfect solution to this and highly recommended.

For example, you could have this:

local color1 = { 10, 10, 10 }  
local color2 = { 20, 20, 30 }  

That’s fine for just a few colors, but let’s say you have 100 of them (ridiculous exaggeration most likely, but just for example). It’s better and more memory-efficient to nest them like this:

local colors = {  
 color1 = { 10, 10, 10 },  
 color2 = { 20, 20, 30 }  
}  

It’s also easier for organizational purposes, and simple to call the values by name like:

colors[color1]

Basically, you can almost never do “too much” in terms of memory optimization. I’m not suggesting you spend weeks of time on the task, but it’s valuable to learn best practices from the start with Lua. Nesting is one of them. :wink:

Brent Sorrentino
[import]uid: 9747 topic_id: 33277 reply_id: 132222[/import]

Wow thanks for the reply! I had no idea that nested tables we more efficient. In that case, I will learn them and love them, and use them.

Thanks! [import]uid: 191855 topic_id: 33277 reply_id: 132229[/import]

Wow thanks for the reply! I had no idea that nested tables we more efficient. In that case, I will learn them and love them, and use them.

Thanks! [import]uid: 191855 topic_id: 33277 reply_id: 132230[/import]

Hmmm… well, the “short answer” is, the fewer up-referenced tables and variables you have in memory, the better. In fact, Lua will choke if you have too many of them in one module. Nesting is a perfect solution to this and highly recommended.

For example, you could have this:

local color1 = { 10, 10, 10 }  
local color2 = { 20, 20, 30 }  

That’s fine for just a few colors, but let’s say you have 100 of them (ridiculous exaggeration most likely, but just for example). It’s better and more memory-efficient to nest them like this:

local colors = {  
 color1 = { 10, 10, 10 },  
 color2 = { 20, 20, 30 }  
}  

It’s also easier for organizational purposes, and simple to call the values by name like:

colors[color1]

Basically, you can almost never do “too much” in terms of memory optimization. I’m not suggesting you spend weeks of time on the task, but it’s valuable to learn best practices from the start with Lua. Nesting is one of them. :wink:

Brent Sorrentino
[import]uid: 9747 topic_id: 33277 reply_id: 132222[/import]

Wow thanks for the reply! I had no idea that nested tables we more efficient. In that case, I will learn them and love them, and use them.

Thanks! [import]uid: 191855 topic_id: 33277 reply_id: 132229[/import]

Wow thanks for the reply! I had no idea that nested tables we more efficient. In that case, I will learn them and love them, and use them.

Thanks! [import]uid: 191855 topic_id: 33277 reply_id: 132230[/import]