View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > ArrayInsertAt PreviousNext

ArrayInsertAt

Inserts a value into an array. Array elements whose indexes are greater than the new position are incremented by one. The array length increases by one.

True, on successful completion.

Array functions

ArrayInsertAt(array, position, value)

ArrayDeleteAt

ColdFusion MX:

Parameter

Description

array

Name of an array

position

Index position at which to insert value

value

Value to insert

If this function attempts to insert an element at position 0, or specifies a value for position that is greater than the size of array, this function throws an InvalidArrayIndexException error.

<h3>ArrayInsertAt Example</h3><p>
<!--- create a new array --->
<cfset DaysArray = ArrayNew(1)>
<!--- populate an element or two --->
<cfset DaysArray[1] = "Monday">
<cfset DaysArray[2] = "Tuesday">
<cfset DaysArray[3] = "Thursday">
<!--- add an element before position 3 --->
<p>Add an element before position 3:
   <cfoutput>#ArrayInsertAt(DaysArray,3,"Wednesday")#</cfoutput>
<p>Now output the array as a list:
<cfoutput>#ArrayToList(DaysArray)#</cfoutput>
<!--- The array now has four elements. Element 3, "Thursday", has become element four --->

Contents > CFML Reference > ColdFusion Functions > ArrayInsertAt PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


RogerDogerCF said on Apr 2, 2004 at 4:30 PM :
How do you insert into a 2D array with arrayinsertat ?
halL said on Apr 6, 2004 at 7:30 AM :
As a general rule, ColdFusion Arrayxxx functions (with the exception of ArrayNew) work on one dimensional of arrays or one dimension of a multidimensional array.
To apply the ArrayInsert function to a multidimensional array, you must specify all but the last index in the first paramaeter.
For example, the following code inserts an element at myarray[2][4]:

<cfset arrayInsertAt(myarray[2], 4, "test")>

Also, please note that the following statement in the Description section is inaccurate:

"Array elements whose indexes are greater than the new position are incremented by one."

The correct statement is

"Array elements whose indexes are equal to or greater than the new position are incremented by one."

This is shown by the following code:

<cfscript>
myarray=arraynew(2);
myarray[2][4]="original element 2,4";
myarray[2][5]="original element 2,5";
</cfscript>
<cfdump var="#myarray#"><br>
<cfset arrayinsertat(myarray[2], 4, "inserted element 2,4")>
<cfdump var="#myarray#">
Robin Hilliard said on Jan 13, 2005 at 5:33 PM :
"Array elements whose indexes are greater than the new position are incremented by one"

should read

"Array elements whose indexes are greater than or equal to the new position are incremented by one"

otherwise it's unclear whether inserting at x inserts before or after the current element at x.

Cheers,
Robin
TinMan75 said on Aug 23, 2007 at 12:58 PM :
If i try to access an array element that is in between 1 and 6 i will get a

"Element 2 is undefined in a Java object of type class coldfusion.runtime.Array referenced as "

error. How do i check to see if there is a value there or not? CF doesn't init these empty elements to null?

As you can see the array length returns 6. If i try to loop over the array an error will occur.

<cfset array = ArrayNew(1)>

<cfset array[1] = "hi">
<cfset array[6] = "bye">

<cfoutput>
#ArrayLen(array)#
</cfoutput>

<cfloop index="i" from="1" to="#Arraylen(array)#">
<cfoutput>
#array[i]#
</cfoutput>
</cfloop>
halL said on Aug 24, 2007 at 6:34 AM :
In response to TinMan75:

ColdFusion arrays are sparse.
If you don't assign a value to an array elment, the element does not exist.
Unfortunately, you cannot use IsDefined to test the existence of an element.
Instead, use a try catch block, as in the following example, which I tested on CF 7 and 8, but not 6.1.

<cfset array1 = ArrayNew(1)>

<cfset array1[1] = "hi">
<cfset array1[6] = "bye">

<cfoutput>
Array Length: #ArrayLen(array1)#<br>
<br>

</cfoutput>

<cfloop index="i" from="1" to="#Arraylen(array1)#">
<!--- <cfif isDefined("array1[2]")> --->
<cfoutput>
<cftry>
#i#: #array1[i]#<br>
<cfcatch type="any">
undefined<br>
</cfcatch>
</cftry>
</cfoutput>
</cfloop>
halL said on Aug 24, 2007 at 6:54 AM :
I forgot to mention in my last note: In ColdFusion 8 we added an ArrayIsDefined(array, index) function that tests the existence of an array element. So in ColdFusion 8 you can avoid the try/catch block as follows:

<cfset myArray = ArrayNew(1)>

<cfset myArray[1] = "hi">
<cfset myArray[6] = "bye">

<cfoutput>
Array Length: #ArrayLen(myArray)#<br>
<br>

<cfloop index="i" from="1" to="#Arraylen(myArray)#">
<cfif ArrayIsDefined(myArray, i)>
#i#: #myArray[i]#<br>
</cfif>
</cfloop>
</cfoutput>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functia7.htm