Understanding Dynamic Array Sizes ColdFusion Help

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(2) .

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


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[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[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
• 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

TAB l E 1 4 . 3:

TAB l E 1 4 . 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


TAB L E 1 4.4:

TAB L E 1 4.4:

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.


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

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


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.


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


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


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

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

Posted on November 16, 2015 in Working with cold Fusion Data Structure

Share the Story

Back to Top
Share This