Be careful when using ArrayDeleteAt. When an element is deleted, ColdFusion recalculates index positions. For example, in an array that contains the months of the year, deleting the element at position 5 removes the entry for May. After this, to delete the entry for June, you would delete the element at position 5 (not 6). Here is an example to demonstrate this behavior:
<cfset myArray = ArrayNew(1)>
<cfset myArray[1] = "Jan">
<cfset myArray[2] = "Feb">
<cfset myArray[3] = "Mar">
<cfset myArray[4] = "Apr">
<cfset myArray[5] = "May">
<cfset myArray[6] = "Jun">
<cfloop from="1" to="#ArrayLen(myArray)#" index="i">
<cfset arrayDeleteAt(myArray,5)>
</cfloop>
If you execute the above, you get:
Cannot insert/delete at position 5.
The array passed has 4 indexes. Valid positions are from 1 to 4.
Possibly related:
- Found or Not Found, That is The Question. Do You Have The Answer?
- Flash CFFORM Gotcha
- NO_DATA_FOUND Gotcha
- LOBs Gotcha in ColdFusion
- What you Ought to Know About CASE in Oracle PL/SQL
Tagged gotcha | Post a Comment


















That’s why you should always loop backwards when performing deletes in an array:
<cfloop from=”#ArrayLen(myArray)#” to=”1″ step=”-1″ index=”i”>
June 23rd, 2005, at 12:02 pm #<cfset arrayDeleteAt(myArray,5)>
</cfloop>
Great tip Martin. Thanks.
June 23rd, 2005, at 1:04 pm #