Populating Arrays with Data  
 
 

One-dimensional arrays can store any values, including queries and other arrays. You can use a number of functions to populate an array with data, including ArraySet, ArrayAppend, ArrayInsertAt, and ArrayPrepend. These functions are useful for adding data to an existing array. In addition, several basic techniques are important to master:

  • Populating an array with ArraySet
  • Populating an array with CFLOOP
  • Populating an array from a query
 
 
  Populating an array with ArraySet  
 
 

You can use the ArraySet function to populate a 1D array, or one dimension of a multi-dimensional array, with some initial value such as an empty string or 0 (zero). This can be useful if you need to create an array of a certain size, but don't need to add data to it right away. Array indexes need to contain some value, such as an empty string, in order to be referenced.

Use ArraySet to initialize all elements of an array to some value:

ArraySet (arrayname, startrow, endrow, value)

This example initializes the array myarray, indexes 1 to 100, with an empty string.

ArraySet (myarray, 1, 100, "")
 
 
  Populating an array with CFLOOP  
 
 

A common and very efficient method for populating an array is by creating a looping structure that adds data to an array based on some condition using CFLOOP.

In the following example, a simple one-dimensional array is populated with the names of the months using a CFLOOP. A second CFLOOP is used to output data in the array to the browser.

<CFSET months=ArrayNew(1)>

<CFLOOP INDEX="loopcount" FROM="1" TO="12">

    <CFSET months[loopcount]=MonthAsString(loopcount)>

</CFLOOP>

<CFLOOP INDEX="loopcount" FROM="1" TO="12">

        <CFOUTPUT>
            #months[loopcount]#<BR>
        </CFOUTPUT>

</CFLOOP>


 
 
BackUp LevelNext
 
 

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