Inserting into multiple dimensional table

I am at a loss here, I’m trying to figure out what seemed like a simple issue, but can’t seem to figure it out.

local myTable = {}  
  
myTable[1].ID = 0  

Even if I add some objects to the table, I cannot seem to insert to the table past the first dimension. If I use table.insert() it works fine, but that’s not efficient.

If I create an object first, like:

myObject.ID = 10  

Then set myTable[1] = myObject, it works fine, and that’s how I typically do it anyway, but I am curious why I can’t seem to use [x]. or [x][x] syntax. [import]uid: 160288 topic_id: 33323 reply_id: 333323[/import]

local myTable = {}  
   
myTable[1].ID = 0  
  

The problem here is that you are trying to reference a property “ID” on a object which doesn’t exist yet “myTable[1]”. You have created the object “myTable”, but not the object “myTable[1]”.

So you can change the above to:

local myTable = {}  
myTable[1] = {} -- here we create myTable[1]  
myTable[1].ID = 0 -- now you should be able to use myTable[1].ID  

myTable[1][1] wouldn’t do anything yet, unless you have set the second [1] to reference an object such as another table.

In the second part of your message you are actually already doing this, although you are creating the object separately, then making it part of the table.

[import]uid: 84115 topic_id: 33323 reply_id: 132318[/import]

Thanks! [import]uid: 160288 topic_id: 33323 reply_id: 132320[/import]

Any way to do this without having to initialize each time you insert a new object?

I prefer to use the insert named object method anyway, but I can’t seem to get my head around this.

I understand when I do t[1].ID I am creating an object in the “1” table. But is there a way to dimension it ahead of time, so I can add t[1] = {id=1, x=0, y=4} ; t[2] = {id =2, x=3,y=5} without doing a t[1] = {} t[2] = {} and so on?

Or is the best way just doing

myObject = {id=1, x=0, y=5}
t[1] = myObject

Is there a way to do it without doing the object method that avoids all the {}? [import]uid: 160288 topic_id: 33323 reply_id: 132362[/import]

How goes?
Your example of “dimension it ahead of time” is perfectly valid, and should work fine.

t[1] = {id=1, x=0, y=4}

is a much better way than:

t[1] = {}
t[1].id = 1
t[1].x = 0
etc.

Of course, when you say “I am creating an object in the ‘1’ table”, you mean in the first (1) *position* of that table, correct? That’s what happens… you’re not creating an actual table with a NAME of 1. Most likely this is what you meant, but I want to make sure. Also, of course, you write “id” and “ID” in your example, while Lua is case-sensitive… again, probably just a typo in your example, but worth mentioning if not. :slight_smile:

Brent
[import]uid: 9747 topic_id: 33323 reply_id: 132384[/import]

Hey Brent,

It wasn’t specifically [1], it was

t[1].id = 10  
t[1].x = 1  
t[2].id = 20  
t[2].id = 2  
t[3].id = 30  
t[3].id = 3  

In the past, I always created objects for 1,2,3 and just assigned them to the table. This is cleaner and more readable, but I was talking to someone about this, and I couldn’t figure out why it wasn’t working for him until it was mentioned about initializing the [1] table. But seeing as that’s only one entry, that would require an insertion for every insertion.

Table.insert solves the problem but is very inefficient and want to avoid it in general.

Typically I would do myobject = {id =10, x= 1} and just add that to the main table and all is well. But I am curious how to handle this syntactically without using variable objects. [import]uid: 160288 topic_id: 33323 reply_id: 132385[/import]

local myTable = {}  
   
myTable[1].ID = 0  
  

The problem here is that you are trying to reference a property “ID” on a object which doesn’t exist yet “myTable[1]”. You have created the object “myTable”, but not the object “myTable[1]”.

So you can change the above to:

local myTable = {}  
myTable[1] = {} -- here we create myTable[1]  
myTable[1].ID = 0 -- now you should be able to use myTable[1].ID  

myTable[1][1] wouldn’t do anything yet, unless you have set the second [1] to reference an object such as another table.

In the second part of your message you are actually already doing this, although you are creating the object separately, then making it part of the table.

[import]uid: 84115 topic_id: 33323 reply_id: 132318[/import]

Thanks! [import]uid: 160288 topic_id: 33323 reply_id: 132320[/import]

Any way to do this without having to initialize each time you insert a new object?

I prefer to use the insert named object method anyway, but I can’t seem to get my head around this.

I understand when I do t[1].ID I am creating an object in the “1” table. But is there a way to dimension it ahead of time, so I can add t[1] = {id=1, x=0, y=4} ; t[2] = {id =2, x=3,y=5} without doing a t[1] = {} t[2] = {} and so on?

Or is the best way just doing

myObject = {id=1, x=0, y=5}
t[1] = myObject

Is there a way to do it without doing the object method that avoids all the {}? [import]uid: 160288 topic_id: 33323 reply_id: 132362[/import]

How goes?
Your example of “dimension it ahead of time” is perfectly valid, and should work fine.

t[1] = {id=1, x=0, y=4}

is a much better way than:

t[1] = {}
t[1].id = 1
t[1].x = 0
etc.

Of course, when you say “I am creating an object in the ‘1’ table”, you mean in the first (1) *position* of that table, correct? That’s what happens… you’re not creating an actual table with a NAME of 1. Most likely this is what you meant, but I want to make sure. Also, of course, you write “id” and “ID” in your example, while Lua is case-sensitive… again, probably just a typo in your example, but worth mentioning if not. :slight_smile:

Brent
[import]uid: 9747 topic_id: 33323 reply_id: 132384[/import]

Hey Brent,

It wasn’t specifically [1], it was

t[1].id = 10  
t[1].x = 1  
t[2].id = 20  
t[2].id = 2  
t[3].id = 30  
t[3].id = 3  

In the past, I always created objects for 1,2,3 and just assigned them to the table. This is cleaner and more readable, but I was talking to someone about this, and I couldn’t figure out why it wasn’t working for him until it was mentioned about initializing the [1] table. But seeing as that’s only one entry, that would require an insertion for every insertion.

Table.insert solves the problem but is very inefficient and want to avoid it in general.

Typically I would do myobject = {id =10, x= 1} and just add that to the main table and all is well. But I am curious how to handle this syntactically without using variable objects. [import]uid: 160288 topic_id: 33323 reply_id: 132385[/import]