Arrays

Team-Fly    

ColdFusion® MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 10.  Using Lists, Arrays, and Structures


Arrays are a type of data structure used to store information, and they are common to almost all programming languages. Like lists, arrays can be used to store multiple values as a single variable. Instead of being stored in a simple string, however, ColdFusion stores arrays in memory as something called complex data. Additionally, whereas lists are only capable of storing simple values like strings and numbers, arrays are able to store complex data like queries, structures, and even other arrays.

Because arrays are stored in memory, access to information stored in an array is usually quicker than parsing through a list to find a value. Arrays also come in one, two, or three dimensions. These dimensions determine how many storage areas are available. Let's take a look at a simple one-dimensional array.

Creating Arrays

You create an array by using the ArrayNew() function (shown later in Table 10.2) and specifying the dimension as the argument for the function. For example, the following code would create a new one-dimensional array called aDepts. Note that we're using an "a" as a variable prefix to denote that the Depts variable is an array (see the "Whats In a Name?" sidebar in Step 2).

 <CFSET aDepts=ArrayNew(1)>

One-dimensional arrays are very similar to lists. They store data values, or elements, in a tablelike structure. These array elements are referenced by their numerical order, or index, within the array. Figure 10.3 shows a simple one-dimensional array containing our department information.

Figure 10.3. A one-dimensional department array.

graphics/10fig03.gif

You can populate element values in an array by using the <CFSET> tag and referencing the element's index value. For example, the following code would enter our department information into the array:

 <CFSET aDepts[1]="Marketing">  <CFSET aDepts[2]="Operations">  <CFSET aDepts[3]="Sales">  <CFSET aDepts[4]="Technology">

You cannot simply display an entire array the way we did with our list. To display array information, you have to refer to the element by its array index. Array indexes represent the numerical position of the element; these numbers are surrounded by square brackets. So we could output Sales by using the following code:

 <CFOUTPUT>#aDepts[3]#</CFOUTPUT>

NOTE

If you have used arrays in other programming languages, note that ColdFusion indexes start with 1. Many other programming languages start array indexes with 0.


Using <CFDUMP>

The <CFDUMP> tag is a great way to troubleshoot arrays as you work with them. It is also a great tool to help you conceptualize what is happening as you learn about arrays.

The <CFDUMP> tag can output the contents of simple variables, arrays, structures, and queries in an easy-to-understand table. To use <CFDUMP>, simply use the VAR attribute to indicate the variable you would like to dump, as follows:

 <CFDUMP VAR="#aDepts#">


    Team-Fly    
    Top
     



    ColdFusion MX. From Static to Dynamic in 10 Steps
    ColdFusion MX: From Static to Dynamic in 10 Steps
    ISBN: 0735712964
    EAN: 2147483647
    Year: 2002
    Pages: 140
    Authors: Barry Moore

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net