The capability of ColdFusion arrays to change size dynamically has some implications that need to be understood. The essential issue is this: An array is a continuous series of elements With the first index being 1 and no gaps in the series of element indexes.

What does this mean? First, an array with the following elements (and no other elements) is a valid array:

NameofArray[l)

NameofArray(2) .

NameofArray(3)

However an array with the following elements (and no other elements) is not valid:

NameofArray[l)

NameofArray(2)

NameofArray(4)

The latter is not valid because a gap exists in the index numbering: Where is Nameof- Array[3)?

This distinction is important when manipulating arrays. For instance consider an array with the following elements:

NameofArray[l)-“One”

NameofArray[2)-“Two’

NameofArray[3)-“Three’

NameofArray[4]-” Four’

What happens if you delete the third element (you will learn to do this later in this chapter when we discuss the Ar rayDe 1eteAt function)? First, the element is deleted. Next, all subsequent indexes are adjusted by 1 so that no gaps exist in the numerical sequence of the indexes

ArrayName[l)-‘One’

ArrayNa.e(2)-“Two’

ArrayName[3]-‘ Four

**Using Array Fundions**

As with lists ColdFusion offers several functions for working with and manipulating arrays. You have already seen the ArrayNew and ArraySet functions in action, but the following functions are also available:

• ArrayAppend

• ArrayPrepend

• ArrayDeleteAt

• ArraylnsertAt

• ArrayMax

• ArrayMin

• ArraySum

• ArrayAvg

• ArraySwap

• ArraySort

• ArrayClear

• ArrayResize

• ArrayIsEmpty

• IsArray

• ListToArray

• ArrayToList

Some of these functions should be familiar because of the corresponding list functions you learned about earlier these array functions are described in Table 14.3

The remaining functions don’t have direct parallels among the list functions. You will consider them in more detail in the following sections

**Functions for Processing Values Stored in an Array**

There are several functions for processing the values stored in an array. These are

**ArrayMax** . Returns the largest numerical value stored in an array

**ArrayMin** Returns the smallest numerical value stored in an array

**ArraySum** Returns the sum of all numerical values stored in an array

**ArrayAvg** Returns the average of all numerical values stored in an array

The syntax of all these functions is simple, taking the form Functi onName(Array).

An important point to understand is that only arrays with strictly numerical values can be processed by these four functions. Any attempt to process an array with one or more non-numeric values will generate an error

Therefore the following three-element array can be processed by these four functions

ArrayName[l) – 1

ArrayName[2) – 2

ArrayName[3) – 3

but the following array cannot be handled by these functions because of the string valuein the second element

ArrayName[l) – 1

ArrayName[2] – ‘two’

ArrayName[3] – 3

Table 14.4 shows the results returned by each function when processing the following array:

ArrayName[l] – 1

ArrayName[2] – 3

ArrayName[3] – 2

**Functions for Adjusting the Position of Elements in an Array**

Two functions are available to change the place of elements in an array:

**ArraySwap** Swaps two elements in an array

**ArraySort** Sorts the elements in an array

Because these two functions work in notably different ways we will explain them separately.

**ArraySwap**

The ArraySwap function swaps the contents of two elements in an array. For instance consider an array with the following elements and values:

ArrayName [1]-1

ArrayName[2]-2

ArrayName[3]-3

ArrayName[4]-4′

If you swap the second and fourth elements, the resulting array has element values as follows:

ArrayName[l]-l

ArrayName[2]-4

ArrayName[3]-3

ArrayName[4]-2

To do this, Array·Swap expects the array name ‘arid the indexes of the two elements to swap: ArraySwap(Array, Pos it; on1, Pos it ion2). The ArraySwap function directly alters the specified array and then returns the Boolean value of True upon successful completion of the swap

Therefore, you execute the preceding swap with the following code:

<CFSET Result – ArraySwap(ArrayName,2,4»

The swapped array is still called ArrayName.

**ArraySort**

The ArraySort function enables the elements of an array to be sorted in different ways including numerically and alphabetically and in ascending or descending order. To make this happen, the function takes three arguments: an array name, a sort type, and an optional sort order. The function directly manipulates the specified array. The syntax for the function is ArraySort(Array, SortType. SortOrder).

The sort type must be one of the following three values:

**numeric** Sorts numerically

**text **Sorts alphabetically, placing uppercase before lowercase

**textnocase** Sorts alphabetically, ignoring case

The most significant distinction is between text and textnocase. In the first instance elements are sorted alphabetically for all uppercase letters and then for all lowercase letters so the following series of elements

a

b

A

B

ends up as follows when sorted with the text sort type:

A

B

,a

b

In contrast, textnocase ignores case, and the resulting sorted series would be:

a

A

b ”

B

Let’s consider an example: Say you want to sort the list of students from our earlier example ignoring case. You would use the following:

<CFSET Result – ArraySort(students,’textnocase’>

Like ArraySwap ArraySort directly alters the array and then returns the Boolean value of True upon successful completion of the sorting.

Another aspect of sorting is the sort order. By default, all sorting is done in,ascending order. However, you have the choice of directly specifying one of two sorting orders as follows

**asc** Sort in ascending order

**desc** Sort in descending order

Therefore, to sort the same array of students in descending order, you would use:

<CFSET Result – ArraySort(students,’textnocase’ ,’desc’>

**Other Array Fundions**

In addition to the functions we have already covered, there are several other array functiORSthat don’t fit together in any clear grouping. These are as follows:

**ArrayClear** Deletes all data in an array

**ArrayResize** Resizes an array to a specified minimum number of elements

**ArraylsEmpty** Checks whether an array is empty

**IsArray** Checks whether a given data structure is an array and, optionally whether it is an array of a specified dimension

**ListToArray** Converts a list to an array

**ArrayToList** Converts an array to a list