# A Table of Tables [RESOLVED]

Greetings. I want to have a table of tables but I dont know what the syntax for accessing things that are in the second liar of tables . I searched the Lua refrence manual but its probley wouldent be the kind of thing that would be in there. Here’s my code (its in a for loop).

``````self.itemBorderTable[i] = {vec4(pos.x - self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x - self.itemWidth/2,
pos.y - self.itemHeight/2),

vec4(pos.x + self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x + self.itemWidth/2,
pos.y - self.itemHeight/2),

vec4(pos.x - self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x + self.itemHeight/2,
pos.y + self.itemHeight/2),

vec4(pos.x - self.itemWidth/2,
pos.y - self.itemHeight/2,
pos.x + self.itemHeight/2,
pos.y - self.itemHeight/2)}
``````

Can someone please give me an example of how I’d acces the differnt vecs?

@Goatboy76
it’s just the same sintax as how you’d create a ‘normal’ table

here’s your example but with ‘other vecs’

``````self.itemBorderTable[i] = {
{pos.x - self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x - self.itemWidth/2,
pos.y - self.itemHeight/2},

{pos.x + self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x + self.itemWidth/2,
pos.y - self.itemHeight/2},

{pos.x - self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x + self.itemHeight/2,
pos.y + self.itemHeight/2},

{pos.x - self.itemWidth/2,
pos.y - self.itemHeight/2,
pos.x + self.itemHeight/2,
pos.y - self.itemHeight/2}  }
``````
``````tbl={{a=1,b=2},{a=3,b=4},{a=4,b=8}}
print(tbl[2][1]) --> 3
print(tbl[3][2]) --> 8
``````

@Goatboy76 Here’s your example with values that I put in and 2 different ways to access the values.

``````
function setup()
tab={}

tab[1]={vec4(111,112,113,114),
vec4(121,122,123,124),
vec4(131,132,133,134),
vec4(141,142,143,144)}

tab[2]={vec4(211,212,213,214),
vec4(221,222,223,224),
vec4(231,232,233,234),
vec4(241,242,243,244)}

-- using direct access
print(tab[1][1].x,tab[1][1].y,tab[1][1].z,tab[1][1].w)
print(tab[1][2].x,tab[1][2].y,tab[1][2].z,tab[1][2].w)
print(tab[1][3].x,tab[1][3].y,tab[1][3].z,tab[1][3].w)
print(tab[1][4].x,tab[1][4].y,tab[1][4].z,tab[1][4].w)
print()

print(tab[2][1].x,tab[2][1].y,tab[2][1].z,tab[2][1].w)
print(tab[2][2].x,tab[2][2].y,tab[2][2].z,tab[2][2].w)
print(tab[2][3].x,tab[2][3].y,tab[2][3].z,tab[2][3].w)
print(tab[2][4].x,tab[2][4].y,tab[2][4].z,tab[2][4].w)
print()

-- using a loop
for a,b in pairs(tab) do
for c,d in pairs(b) do
print(c,d.x,d.y,d.z,d.w)   -- changed
end
print()
end
end

``````

@Goatboy76 I made an update to the above example.

@Ignatz Maybe Im just missing something but your example prints nil. I cant see why because it uses the same syntax as @dave1707 's

@dave1707 Cool example. I don’t know why it works with vecs but not with variables.

Thank you both.

@Goatboy76 - my mistake, should be

``````tbl={{1,2},{3,4},{4,8}}
print(tbl[2][1]) --> 3
print(tbl[3][2]) --> 8
``````

I shouldn’t have named the table elements

@Goatboy76 I’m not sure what you mean by “it works with vecs but not with variables”. Here is an example using the same code you showed in your first post. I added code to put values in your table and to show you how to access each value. Are you saying that instead of using vec4(…), you want to use a variable name (16 of them), one for each entry.

``````
function setup()
i1=item(200,100)    -- initialize values
i1:createTable(1,vec2(50,50))    -- create the table entry
i1:printTable()    -- print the table values
print()
i2=item(500,300)
i2:createTable(1,vec2(200,100))
i2:createTable(2,vec2(400,200))
i2:printTable()
end

item=class()

function item:init(w,h)
self.itemWidth=w
self.itemHeight=h
self.itemBorderTable={}
end

function item:createTable(i,pos)

-- code from the original post

self.itemBorderTable[i] =
{vec4(pos.x - self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x - self.itemWidth/2,
pos.y - self.itemHeight/2),

vec4(pos.x + self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x + self.itemWidth/2,
pos.y - self.itemHeight/2),

vec4(pos.x - self.itemWidth/2,
pos.y + self.itemHeight/2,
pos.x + self.itemHeight/2,
pos.y + self.itemHeight/2),

vec4(pos.x - self.itemWidth/2,
pos.y - self.itemHeight/2,
pos.x + self.itemHeight/2,
pos.y - self.itemHeight/2)
}
end

function item:printTable()
for a,b in pairs(self.itemBorderTable) do -- access the itemBorderTable
for c,d in pairs(b) do  -- access each vec4 entry
print(d.x,d.y,d.z,d.w)  -- access each vec4 value
end
print()
end
end

``````

@dave1707 Thanks for the example, it helps a lot.

Sorry I haven’t responded to this thread, just forgot that I dint. I think that I got it worked out now, thanks for the help.