Skip to main content
Inspiring
April 28, 2008
Question

Arrays doing my nut in

  • April 28, 2008
  • 2 replies
  • 307 views
I`m using a tutorial on arrays but don´t understand why the author populates the arrays with a value of "0". Second I tried to enter a new value <cfset gallery[idx][3] = "0"> will that then create a third dimension in the array making in 3d? Thanks for sorting out my head on this one, arrays are difficult to understand but I am nearly there!!

This topic has been closed for replies.

2 replies

Inspiring
April 28, 2008
Ian's answered your question, so I'll leave that.

Does the author of this tutorial explain why they're suggesting putting a
query recordset into a two-dimensional array? It doesn't seem like a very
sensible thing to be doing, IMO.

I could understand them suggesting an array of structs, or [just leaving it
as-is]. But not a 2-D array.

Is there a URL for this tutorial?

--
Adam
Inspiring
April 28, 2008
Hydrowizard wrote:
> I`m using a tutorial on arrays but don't understand why the author populates
> the arrays with a value of "0".

It is the author's way of handling the condition of the query not
returning data to populate all the array elements. One school of
thought is that all array elements should be populated. ColdFusion does
not do this by default so the author wrote a loop to do it. There are
also CF functions to accomplish the same thing.

> Second I tried to enter a new value <cfset
> gallery[idx][3] = "0"> will that then create a third dimension in the array
> making in 3d?

No, that would add a third element to the second index. Think of a 2d
array as a table of rows and columns. The author created an array|table
of 10 rows by 2 columns (or 10 columns by 2 rows if you prefer). If you
added gallery[idx][3] = "0", then you would have a 10 x 3 array|table.

To make your array 3d, you would need to add another index to it. In CF
you can either do this in the definition, i.e. <cfset my3dAry =
arrayNew(3)>. Or by adding an array value to an existing array element;
<cfset my2dAry[idx][idy] = arrayNew(1)>. This will then create a 'cube'
of row by columns by sheets. You would then access this 3d array with
three indexes. I.E. my3dAry[idx][idy][idz].

Other languages are not so flexible in the creation of arrays. They
require you to define an array right at the beginning, they must have
the same number of elements in all dimensions and are often hard, if not
impossible, to resize later.

CF does not care, you can create and morph your array as much as you
want and have different number of rows, columns or sheets at different
points.

Once you get all this, if you can wrap your head around arrays with 4th,
5th and even 6th dimensions you can consider yourself a master.