Hi.

Personally I don't see anything different between your first way of
doing it and the second. I would thing that the second way is simply
doing the work CF would be doing behind the scenes anyway. The main
problem you are going to have is that if your code tries to reference
an array element that you have not defined, in this case [2][3] you
will get a CF Error about it not existing. Personally I would say best
practise would be to set the same number of elements in each row of
the array, but leave the one's you are not using blank. So your
example would end up being:

<cfscript>
      my_test = arrayNew(2);


      my_test[1][1] = "foo";
      my_test[1][2] = "bar";
      my_test[1][3] = "foobar";


      my_test[2][1] = "foo1";
      my_test[2][2] = "foo2";
      my_test[2][3] = "";
</cfscript>

Then if you try and reference [2][3] you no longer get a CF error.

Andrew.

----- Original Message -----
From: Charlie Griefer <[EMAIL PROTECTED]>
Date: Wed, 15 Sep 2004 00:23:25 -0700
Subject: 2d array question
To: CF-Talk <[EMAIL PROTECTED]>

ok...in ColdFusion, a 2d array is a 1d array of 1d arrays.  i think
that's the first sentence in the docs under '2d arrays'.  that's all
well and good.

i'm curious tho...is there any recommendation regarding the 2nd
'dimension' being uniform length?  for example, if I have the
following:

<cfscript>
      my_test = arrayNew(2);


      my_test[1][1] = "foo";
      my_test[1][2] = "bar";
      my_test[1][3] = "foobar";


      my_test[2][1] = "foo1";
      my_test[2][2] = "foo2";
</cfscript>

... is that 'legal'?  or should my_test[2] have a 3rd element in the
2nd dimension?

If i'm in a situation where i'm going to have inconsistent numbers of
elements in the 2nd dimension...is it 'better form' to create 1
dimensional arrays and then manually create a new array within each, a
la:

<cfscript>
      my_test = arrayNew(1);


      my_test[1] = arrayNew(1);
      my_test[1][2] = "foo";
      my_test[1][2] = "bar";
      my_test[1][3] = "foobar";


      my_test[2] = arrayNew(1);
      my_test[2][1] = "foo1";
      my_test[2][2] = "foo2";
</cfscript>

while the 2nd example is certainly the "longer way around", it seems
cleaner, in that each 'nested array' is created independently of the
other, and therefore shouldn't care about different lengths.

maybe i'm just being anal...but i feel like if i create a 2d array,
then there is a distinct 'relationship' between all of the 2nd
dimension values...and they should all be of a uniform length.

is there any 'rule of thumb' as far as this is concerned?

thx!

--
Charlie Griefer

================================================
Marta was watching the football game with me when she said,
"You know, most of these sports are based on the idea of one group
protecting its territory from invasion by another group."
"Yeah," I said, trying not to laugh. Girls are
funny.________________________________
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to