Help Defining three dimensional array

Hi,

I’m having some trouble defining a three dimensional array:

quad = {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}

universe = {

{{quad},{quad},{quad},{quad},{quad},{quad},{quad},{quad}},

{{quad},{quad},{quad},{quad},{quad},{quad},{quad},{quad}},

{{quad},{quad},{quad},{quad},{quad},{quad},{quad},{quad}},

{{quad},{quad},{quad},{quad},{quad},{quad},{quad},{quad}},

{{quad},{quad},{quad},{quad},{quad},{quad},{quad},{quad}},

{{quad},{quad},{quad},{quad},{quad},{quad},{quad},{quad}},

{{quad},{quad},{quad},{quad},{quad},{quad},{quad},{quad}},

{{quad},{quad},{quad},{quad},{quad},{quad},{quad},{quad}}

}

If I try to access universe[1][1][1] it blows up…  I think my declaration is wrong, but I don’t know where.

Thanks, Greg

If 3 -dimensional then
universe = {quad, quad, quad}

if quad is a 8x8 array and I have a 8x8 array of quads how do I define that?

arrays are arrays, soo…

[lua]local row = {1,2,3,4,5,6,7,8}

local quad = {tier, tier, tier, tier, tier, tier, tier, tier}

local quadRow = {quad, quad, quad, quad, quad, quad, quad, quad}

local structure = {quadRow, quadRow, quadRow, quadRow, quadRow, quadRow,quadRow,quadRow}[/lua]

3D Arrays are really just nested tables.

so I can just reference an element using structure[1][1][1]?

Thanks, Greg

Yes, although I recommend you write a recursive function to address it safely.The danger with addressing structure[1][1][1] is that you must be sure structure[1] and structure[1][1] exist. If either one does not, the app will assert. If only the last [1] doesn’t exist, you’ll merely return ‘nil’.

[lua]local function findElement(depth, array)

   if depth > 1 and array[depth] then

     depth = depth - 1

     findElement(depth, array[depth])

  elseif depth == 1 and array[depth] then

     return array[depth]

  else

     return false

  end

[/lua]

This function would only return the element at your chosen depth (ie: 3 [1]s deep) and if any of them don’t exist, it returns false.

If you’re sure the previous layers exist, then structure[1][1][1] is perfectly fine.

Thanks for the help Richard, I’ll give it a try!

Greg

If 3 -dimensional then
universe = {quad, quad, quad}

if quad is a 8x8 array and I have a 8x8 array of quads how do I define that?

arrays are arrays, soo…

[lua]local row = {1,2,3,4,5,6,7,8}

local quad = {tier, tier, tier, tier, tier, tier, tier, tier}

local quadRow = {quad, quad, quad, quad, quad, quad, quad, quad}

local structure = {quadRow, quadRow, quadRow, quadRow, quadRow, quadRow,quadRow,quadRow}[/lua]

3D Arrays are really just nested tables.

so I can just reference an element using structure[1][1][1]?

Thanks, Greg

Yes, although I recommend you write a recursive function to address it safely.The danger with addressing structure[1][1][1] is that you must be sure structure[1] and structure[1][1] exist. If either one does not, the app will assert. If only the last [1] doesn’t exist, you’ll merely return ‘nil’.

[lua]local function findElement(depth, array)

   if depth > 1 and array[depth] then

     depth = depth - 1

     findElement(depth, array[depth])

  elseif depth == 1 and array[depth] then

     return array[depth]

  else

     return false

  end

[/lua]

This function would only return the element at your chosen depth (ie: 3 [1]s deep) and if any of them don’t exist, it returns false.

If you’re sure the previous layers exist, then structure[1][1][1] is perfectly fine.

Thanks for the help Richard, I’ll give it a try!

Greg