Implicit Arrays in ColdFusion 8
by Simon. Average Reading Time: about a minute.
A great new feature of ColdFusion 8 is its new implicit creation of Arrays and Structures. In addition to the updates to operators in ColdFusion, those of you familiar with JavaScript will recognise and welcome these changes.
An array is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type. However, ColdFusion, as we will see, is not strictly typed and therefore allows any data type to be stored in combination. This allows strings, integers, booleans and other complex data types all to be stored in the same array. However, doing this certainly isn’t a good practice as it causes signification complication when accessing the stored data.
Variables of a simple data type commonly only store a single value but, in some situations, it is useful to have a variable that can store a series of related values – using an array. Arrays are described as complex data types because they can hold data in a structured, complex way.
For example, suppose a routine is required that will calculate the average age among a group of six students. The ages of the students could be stored in six integer variables, added together and then divided by 6:
<cfscript> age1 = 19; age2 = 20; age3 = 21; age4 = 22; age5 = 23; age6 = 18; average_age = (age1 + age2 + age3 + age4 + age5 + age6) / 6; writeOutput(average_age); // returns 20.5 </cfscript>
However, a better solution would be to store the data in a six-element array and calculate the average age based upon the sum or the ages and the array length:
<cfscript> age = arrayNew(1); age[1] = 19; age[2] = 20; age[3] = 21; age[4] = 22; age[5] = 23; age[6] = 18; sum_age = 0; //loop over the age array adding age together all ages for (i = 1; i <= arrayLen(age); i++) { sum_age += age[i]; } //calculate the average age average_age = sum_age / arrayLen(age); writeOutput(average_age); // returns 20.5 </cfscript>
This is a few more lines of code, but allows for the flexibility of increasing or decreasing the number of elements (in this case ages) in the array, with no impact upon the core calculation.
NB: Unlike other programming languages, ColdFusion array indexes start from 1 not 0 (zero).
Implicit Arrays
With the introduction of implicit arrays in ColdFusion 8, the creation of arrays is greatly simplified. For example, rather than having to use the arrayNew(1) function, we can now simply do the following:
<cfscript> myArray1 = [1,2,3]; //integers </cfscript>

This means that we can take the age calculation from ealier, and make the code even simpler to write:
<cfscript> //implicity create the age array age = [19,20,21,22,23,18]; sum_age = 0; //loop over the age array adding age together all ages for (i = 1; i <= arrayLen(age); i++) { sum_age += age[i]; } //calculate the average age average_age = sum_age / arrayLen(age); writeOutput(average_age); // returns 20.5 </cfscript>
Further examples
The following code snippets serve to exemplify my earlier comment that arrays can store any data type and indeed, any data type in combination.
Using strings:
<cfscript> myArray2 = ["One","Two","Three"]; //strings </cfscript>

Using complex data types:
<cfscript> myArray3 = [myArray1,myArray2]; //complex types (arrays) </cfscript>

Using implicit structures:
<cfscript> myStruct1 = {firstname="Simon", lastname="Whatley", city="London"}; myStruct2 = {firstname="John", lastname="Doe", city="New York"}; myArray = [myStruct1, myStruct2]; </cfscript>

The last two examples above serve to demonstrate that any complex data type can be used in conjuction with an array. This has not changed between ColdFusion version 7 and 8.
Mixing data types:
Although certainly not good practice, it is possible to mix the data types in an array. For example, we can use an Integer, String and Array as elements within an array, with no problem. However, it is when accessing this data that problems will arise.
<cfscript> myArray4 = [1,"Two",myArray2]; </cfscript>

Although the ColdFusion engine is not strict with regard to what data types are used within an array, always stick to the same type for each element.
Words of Caution
Implicit arrays do have their limitations. For example, you cannot nest implicit array, or indeed struct, creation.
<cfscript> arrayOne = [ arrayTwo = [1,2,3], arrayThree = [4,5,6] ] </cfscript>
The above will throw the following parsing error:
coldfusion.compiler.ParseException: Invalid CFML construct found on line 3 at column 13.
UPDATE: The recent ColdFusion Update now includes the ability to nest implicit arrays.
