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>

Implicit Arrays - Integer Example

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>

Implicit Arrays - String Example

Using complex data types:

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

Implicit Arrays - Variables Example

Using implicit structures:

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

Implicit Arrays - Complex Example

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>

Implicit Arrays - Combined Example

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.

Following on from the first three parts of my “Using JavaScript Operators” in ColdFusion 8 series, where I introduced Arithmetic, Assignment and Logical Operators, it is now the turn of Comparison Operators.

In the past we have been restricted to the more ‘wordy’ operators such as those defined in the first table below.

Operator Description
IS
EQUAL
EQ
Perform a case-insensitive comparison of two values. Return True if the values are identical.
IS NOT
NOT EQUAL
NEQ
Opposite of IS. Perform a case-insensitive comparison of two values. Return True if the values are not identical.
CONTAINS Return True if the value on the left contains the value on the right.
DOES NOT CONTAIN Opposite of CONTAINS. Return True if the value on the left does not contain the value on the right.
GREATER THAN
GT
Return True if the value on the left is greater than the value on the right.
LESS THAN
LT
Opposite of GREATER THAN. Return True if the value on the left is smaller than the value on the right.
GREATER THAN OR EQUAL TO
GTE
GE
Return True if the value on the left is greater than or equal to the value on the right.
LESS THAN OR EQUAL TO
LTE
LE
Return True if the value on the left is less than or equal to the value on the right.

However, with the arrival of ColdFusion 8, the CFML scripting language has been brought into line with other major scripting languages. This change is undoubtedly good for ColdFusion as developers familiar with the ActionScript and JavaScript syntax can now more effectively code ColdFusion and vice-versa.

The following table describes the comparison operators that can be used in ColdFusion 8, albeit used in <cfscript> expressions only:

Operator Description Examples returning true1
Equal (==) If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers; if either operand is a string, the other one is converted to a string

3 == var1
"3" == var1
3 == '3'

Not equal (!=) Returns true if the operands are not equal. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison.

var1 != 4 var1 != "5"

Greater than (>) Returns true if the left operand is greater than the right operand.

var2 > var1

Greater than or equal (>=) Returns true if the left operand is greater than or equal to the right operand.

var2 >= var1 var1 >= 3

Less than (<) Returns true if the left operand is less than the right operand.

var1 < var2

Less than or equal to (<=) Returns true if the left operand is less than or equal to the right operand.

var1 <= var2
var2 <= 5

1 These examples assume that var1 has been assigned the value 3 and var2 has been assigned the value 4.

Below are just a few simple examples of the new ColdFusion operators and their use:

IS EQUAL TO

In the below example, the value of var1 is not equal to var2, therefore the comparison returns false and the else statement is processed, writing the number 4 to the screen.

<cfscript>
var1 = 3;
var2 = 4;
if (var1 == var2) //returns false
	writeOutput(var1);
else
	writeOutput(var2); //writes out 4
</cfscript>

IS NOT EQUAL TO

In the below example, the variable var1 is not equal to 4, therefore the statement returns true, writing 3 to the screen. The second if statement serves to emphasise that ColdFusion is not strictly typed. Therefore comparing var1 to an Integer or a String is possible. ColdFusion handles type conversion in the background.

<cfscript>
var1 = 3;
var2 = 4;
if (var1 != 4) //returns true
	writeOutput(var1); //writes out 3
 
if (var1 != "5") //returns true
	writeOutput(var1); //writes out 3
</cfscript>

IS GREATER THAN

In the below example, var2 is greater than var1, therefore the value of var2, 4, is printed out on screen. Changing the operator to Greater Than or Equal To (>=) would also return the same result. However, using the Less Than (<) and Less Than or Equal To (<=) operators would return false and not write a value to the screen.

<cfscript>
var1 = 3;
var2 = 4;
if (var2 > var1) //returns true
	writeOutput(var2); //writes out 4
</cfscript>