Referencing Elements in Dynamic Arrays  
 
 

Unlike the C language, in ColdFusion, array indexes are counted starting with position 1. In C, array indexes start with zero, so position 1 would be referenced as firstname[0]. In ColdFusion, position one is referenced as firstname[1].

Let's add to the current firstname array example. For 2D arrays, you reference an index by specifying two coordinates: myarray[1][1].

<!--- This example adds a 1D array to a 1D array --->

<CFSET firstname=ArrayNew(1)>

<CFSET firstname[1]="Coleman">
<CFSET firstname[2]="Charlie">
<CFSET firstname[3]="Dexter">

<!--- First, declare the array --->

<CFSET fullname=ArrayNew(1)>

<!--- Then, add the firstname array to
index 1 of the fullname array --->

<CFSET fullname[1]=firstname>

<!--- Now we'll add the last names for symmetry --->

<CFSET fullname[2][1]="Hawkins">
<CFSET fullname[2][2]="Parker">
<CFSET fullname[2][3]="Gordon">

<CFOUTPUT>
    #fullname[1][1]# #fullname[2][1]#<BR>
    #fullname[1][2]# #fullname[2][2]#<BR>
    #fullname[1][3]# #fullname[2][3]#<BR>
</CFOUTPUT>
 
 
  Additional referencing methods  
 
 

You can reference array indexes in the standard way: myarray[x] where x is the index you want to reference. You can also use ColdFusion expressions inside the square brackets to reference an index. The following are valid ways of referencing an array index:

<CFSET myarray[1]=expression>
<CFSET myarray[1 + 1]=expression>
<CFSET myarray[arrayindex]=expression>
 
 
  Calculating an array index  
 
 

As described earlier in Shifting indexes in a dynamic array, array indexes are recalculated whenever data is added, removed, appended, or prepended to an array. Keep this in mind when building an array that will be manipulated in any of these ways.



 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.