Skip to main content
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Inga Lamote
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Inga Lamote

ColdFusion Bug: Arrays, ArrayLen(), And Implicit Structs

By on

I just ran up against a strange bug using ColdFusion arrays and implicit structs. I have experienced and read about several issues with implicit structs before and I would bet that this bug is falling under the same mechanism. The error occurs when I try to create an implicit struct and add it to an array at the same time. However, the bug only occurs if I get the array index of the insert based on the existing length of the array:

<!--- Define array. --->
<cfset arrData = [] />

<!--- Add struct to array. --->
<cfset arrData[ ArrayLen( arrData ) + 1 ] = { Foo = "Bar" } />

This throws the ColdFusion error:

The element at position 2 of dimension 1, of array variable "ARRDATA," cannot be found.

At first, I thought maybe there was something wrong with my indexing math; but, if you separate the calculation out into a separate line:

<!--- Define array. --->
<cfset arrData = [] />

<!--- Get next index. --->
<cfset intIndex = (ArrayLen( arrData ) + 1) />

<!--- Add struct to array. --->
<cfset arrData[ intIndex ] = { Foo = "Bar" } />

... it works just fine. I know I just read something recently on the order of operations that gets carried out with implicit structs, but I am not sure that this applies here as the error seems to be a function of the ArrayLen() evaluation.

Want to use code from this post? Check out the license.

Reader Comments


In addition to your second example actually working, I believe it is also more readable. I find this to be a better coding standard - one I try to follow, at least in other scripting languages. It is similar to looping over the length of an Array.

I would prefer this:

<cfset myArrayLen = ArrayLen(myArray) />

<cfloop from="1" to "#myArrayLen#" index="i">

to this:

<cfloop from="1" to "#ArrayLen(myArray)#" index="i">



Ideally, what I would like to do is actually pass the implicit struct to the ArrayAppend() method:

<cfset ArrayAppend( arrData, { Foo = "Bar" } ) />

... but ColdFusion cannot support this yet. Hopefully in CF9, this improvement will be there. I think it's the best of both worlds.


Hmm, for what you're doing there in the example, wouldn't it make more sense to use ArrayAppend()?

I'm not surprised CFML couldn't handle something like that. Have you tried the same code in CFScript? Somehow I think CFScript will work...


Here's something weird:
<cfset arrData[ ArrayLen( arrData ) + 1 ] = "">

Does work for me, it's only when passing an implied structure that throws it off.

For me I use a simple function to get around this which also works as seen here:

<cffunction name="Echo" access="public" returntype="any" output="false" hint="Just returns the arguments passed to it.">
<cfreturn ARGUMENTS />
<!--- Define array. --->
<cfset arrData = []>

<!--- Get next index. --->
<cfset arrData[ ArrayLen( arrData ) + 1 ] = Echo(Foo="Bar")>



I have not tried this in Script, but I think it's a compile time error, not a runtime error. So, it may work in script, not sure.


Yeah, its a strange error. Implicit creation must be doing something rather odd.


This is the same bug that I've blogged about several times. ColdFusion compiles this code:

<cfset a[arrayLen(a) + 1] = { foo = 2 } />

into this:

<cfset a[arrayLen(a) + 1] = structNew() />
<cfset a[arrayLen(a) + 1].foo = 2 />

This is a bug in ColdFusion's processing of struct literals, nothing to do with arrays. If you execute that code on Railo it'll work just dandy.



Ahhh, you were the one who blogged about it! For some reason I thought it was Elliott Sprehn (I tried Googling it when it happened). Thanks for the clarification.


Hi Ben,

I think you're right, it is a bug. It looks like the array is trying to reference the array at zero.

Try this code:

<cfset arrData = ['']>
<cfset arrData[(arrayLen(arrData)+1) - 1 ] = { Foo = "Bar" }>

<cfdump var="#arrData#"><cfabort>

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel