Susan,

A two dimensional array in CF is actually an array of arrays.

Each of the arrays can end up being a differnt size.

I think that may be your problem.

Kevin Roche
CF User Group Manager
Thames Valley UK

-----Original Message-----
From: Susan McNab [mailto:[EMAIL PROTECTED]
Sent: 07 August 2006 03:19
To: CF-Talk
Subject: CF Arrays


I was getting an error that appeared to be randomly occurring in one of my
sites: "The element at position [n] cannot be found"
Eventually I traced it to the fact that I had been using ArrayLen to set the
number of iterations for a loop when the array size had been expanding. The
code was therefore trying to reference empty array elements and causing the
error.

I knew that CF arrays are "dynamic" and that they can therefore sometimes
expand beyond the minimum number of elements needed. I thought I had been
careful about that though. I realise now that I need to know more about the
internal mechanics of CF arrays.. What tripped me up was that the array was
expanding because an IF statement referred to an outer dimensional array
element that was greater than the original array size. The same thing
happens with cfoutput and possibly other actions that you would expect to
generate an error, but not to change the bounds of the array. This does not
happen with one dimensional arrays, or the inner dimensions of  2D arrays.
With both single and multidimensional arrays an error is generated when you
first make an out of bounds reference to the array. That is what you want,
but why should expanded array elements then persist in memory in the case of
multidimensional arrays? Why should CF need to create extra elements for
this?

It was pretty easy to find a way to cope with the problem in my application,
but I wondered wether this behaviour is correct, and why it has to be this
way. In my case I had been using some multidimensional arrays in application
scope so there was a risk of expanded array sizes eventually affecting
performance. Does any one know what's going on under the hood here?

The code below shows illustrates the effect simply:
<cfset application.a2DArray = ArrayNew(2)>
<cfset application.a2DArray[1][1] = "data11">
<cfset application.a2DArray[1][2] = "data12">
<cfset application.a2DArray[2][1] = "data21">
<cfset application.a2DArray[2][2] = "data22">

<!--- Dump array with 2 x 2 elements --->
<cfdump label="application.a2DArray" var="#application.a2DArray#">

<cftry>
 <cfif application.a2DArray[4][2] eq "something">You would expect this to
generate an error but not to change the array size</cfif>
 <cfcatch type="Any">
  <cfoutput>#cfcatch.message#</cfoutput>
 </cfcatch>
</cftry>

<!--- Array now has with 2 x 2 two elements plus 2 new empty elements --->
<cfdump label="application.a2DArray" var="#application.a2DArray#">



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four 
times a year.
http://www.fusionauthority.com/quarterly

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:248988
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to