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.