Arrays and Structures are considered to be complex data types in ColdFusion. In contrast, simple data types are ones that contain a single piece of data, such as an Integer, String, or Boolean value. A complex data type can contain multiple pieces of data, which, in the case of arrays, are usually related. All the data are referenced under a single variable name. You can think of a complex variable as a variable that contains a collection of other variables inside it. An array maps Integers to arbitrarily typed objects (Integers, Strings, Booleans and Objects) while a structure, or associative array, maps arbitrarily typed objects to arbitrarily typed objects.

Arrays and structures are also known as reference types. A reference is an object containing information which refers to data stored elsewhere, as opposed to containing the data itself. References are fundamental to constructing many data structures and in exchanging information between different parts of a program. References increase flexibility in where objects can be stored, how they are allocated, and how they are passed between areas of code. As long as we can access a reference to the data, we can access the data through it, and the data itself need not be moved. They also make sharing of data between different code areas easier; each keeps a reference to it.

Array and structure reference types are pointers to a memory space. Pointers are the most primitive and error-prone but also one of the most powerful and efficient types of references, storing only the address of an object in memory. Because arrays and structures are pointers to a space in memory, it allows for easy copying of that data. However, when copying an array or structure, there is a distinct difference between how the copy is performed. This difference relates to the concepts of shallow and deep copying.

When copying an array, a deep copy is performed by ColdFusion. This means that the data is copied and a reference to the new copy is created in memory. Therefore, following the example below, a change in the value held at position 1 of array b will not over-write the value held in position 1 of array a as they have different memory references:

<cfscript>
	a = arrayNew(1);
	a[1] = 100;
	b = a; //deep copy for Array
	b[1] = 500;
	writeOutput(a[1]); // the returned result is 100
</cfscript>

We can also use the little-known Java function equals() to demonstrate that the two arrays are not equal (the references are different).

<!--- 
the following returns false
a and b do not refer to the same object
 --->
<cfdump var="#a.equals(b)#">

This is not the case for a structure. When copying a structure, as in the example below, a shallow copy is performed by ColdFusion. This means that a change in b.name will also result in a change of a.name:

<cfscript>
	a = structNew();
	a.name = "Simon Whatley";
	b = a; //shallow copy for Struct
	b.name = "John Doe";
	writeOutput(a.name); // the returned result is John Doe
</cfscript>

Using the equals() method, we can show that the two structures refer to the same object:

<!--- 
the following returns true
a and b refer to the same object
 --->
<cfdump var="#a.equals(b)#">

However, by using the duplicate() function, a deep copy of a structure can be performed:

<cfscript>
	a = structNew();
	a.name = "Wu Mingshi";
	b = duplicate(a); //deep copy for Struct
	b.name = "Ivan Ivanovic";
	writeOutput(a.name); // the returned result is Wu Mingshi
</cfscript>

The above code results in two structures being created.

Arrays and structures although similar, behave very differently. Both are known as reference types which refer to a pointer in memory. Both have the ability to contain multiple values underneath a single variable name. Both use an index to access an individual value, but the index is numeric for arrays and a arbitrary, text value for structures. Arrays are extremely useful for numeric calculations, tabular data, and data sorting. Structures, by their nature, cannot be sorted by value, only by key name. They are commonly for related data, where order is not important and direct access to an individual element is important. Many of ColdFusion’s variable scopes (such as server, application, variables, session, form etc) can be accessed as structures.

New Atlanta is announcing today, at CFUnited Europe, a ColdFusion technology conference in London, U.K., that they will be creating and distributing a free open-source Java Platform, Enterprise Edition (Java EE) version of BlueDragon, their ColdFusion-compatible web application server.

You can read the full release here:

http://www.newatlanta.com/products/bluedragon/open_source/faq.cfm

Will Adobe follow suit with their ColdFusion server technology? They have done similar things with the BlazeDS, so it stands to reason that ColdFusion could follow the same route.

Terrence Ryan sees things differently, however, and has a good article here:

Yawn, BlueDragon Goes Open Source.

In an earlier post I eluded to the implicit creation of arrays in ColdFusion 8. Well, the same can be said of structures.

A structure, also known as an associative array, is a complex data type composed of a collection of keys and a collection of values, where each key is associated with one value (a key-value pair). The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by a structure. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key “Age” is 29 and “City” is “London”, we say that our structure maps “Age” to 29 and “City” to “London”.

Using structures, you can call the array element you need using a string rather than a number, which is often easier to remember. The downside is that these aren’t as useful in a loop because they do not use numbers as the index value.

We can think of an address book as a good example of a structure. The classic way of creating and assigning key-values pairs to a structure, in earlier versions of ColdFusion, would be as follows:

<cfscript>
strPerson = structNew();
strPerson.firstName = "Jean";
strPerson.lastName = "Dupont";
strPerson.city = "Paris";
</cfscript>

Or an alternative method uses array-notation to create the necessary key-value pairs:

<cfscript>
strPerson = structNew();
strPerson["Firstname"] = "Hans";
strPerson["Lastname"] = "Mustermann";
strPerson["Country"] = "Germany";
</cfscript>

NB. When using the array-notation, the key names keep their case. However, running the following code results in the value “France” being overwritten with “Germany”, even though the key name is a different case. This serves to highlight that ColdFusion is not case-sensitive.

<cfscript>
strPerson = structNew();
strPerson["Country"] = "France";
strPerson["COUNTRY"] = "Germany";
</cfscript>

Implicit Structures

With the introduction of implicit structures in ColdFusion 8, the creation of structures is greatly simplified. For example, rather than having to use the structNew() function, we can now simply do the following:

Using strings for values:

<cfscript>
myStruct = {firstname="Simon", lastname="Whatley", city="London"};
</cfscript>

Implicit Structures - Strings as Keys and Values

Using integers for values:

<cfscript>
myStruct = {account_no=12345678, sort_code=123456};
</cfscript>

Implicit Structures - Integers as Values

Using integers as keys:

This example most closely represents an array since arrays have numeric keys.

<cfscript>
myStruct = {10001="John", 10002="Doe", 10003="New York"};
</cfscript>

Implicit Structures - Integers as Keys

The integer could represent the unique identifier of an object, for example, user ID or order ID. Therefore, if we had nested structures like below, 10001 would be the ID of Simon Whatley, whilst 10002 would be the ID of John Doe.

<cfscript>
myStruct1 = {firstname="Simon", lastname="Whatley", city="London"};
myStruct2 = {firstname="John", lastname="Doe", city="New York"};
myStruct3 = {10001=myStruct1, 10002=myStruct2};
</cfscript>

Implicit Structures - Nested Structures

Mixed data types:

It is possible to mix the data types in an structure. For example, we can use an Integer, String and Array as elements within an array, with no problems. Since we need to know the key name before accessing the value, it is also likely we will know the type of the value and will be able to handle it accordingly. However, never assume this is always the case, so type checking is necessary when retrieving the data.

The example below demonstrates the ability to add arrays to structures.

<cfscript>
myArray1 = [1,2,3];
myArray2 = ["One","Two","Three"];
myStruct = {array1=myArray1, array2=myArray2};
</cfscript>

Implicit Structures - Nested Arrays

Structures, by their nature, cannot be sorted by value, only by the key name. They are best for related data, where order is not important and direct access to an individual element is important. Many of ColdFusion’s variable scopes can be accessed as structures, for example, Server, Application, Session and Variables etc.

Words of Caution

Implicit structures do have their limitations. For example, you cannot nest implicit struct, or indeed array, creation.

<cfscript>
myStruct1 = {
	myStruct2 = {
		firstName = "Jean",
		lastName = "Dupont",
		country = "France"
	},
	myStruct3 = {
		firstName = "Juan",
		lastName = "Pablo",
		country = "Spain"
	}
}
</cfscript>

The above will throw the following parsing error:

coldfusion.compiler.ParseException: 
Invalid CFML construct found on line 3 at column 10.

UPDATE: The recent ColdFusion Update now includes the ability to nest implicit structures.

To get around this problem, you can create each structure individually and then use the structure as the value in a key-value pair (as seen in the nested structure example above).

A (possible) strength of ColdFusion is that you can add key-value pairs as many times as is necessary. This is the same for explicit and implicit structure creation. However, the following code and screenshot serves to demonstrate that whether you explicitly or implicitly create a structure, if you duplicate a key, the last key-value pair in the sequence is the one that is represented in the structure:

<cfscript>
myStruct = {
	firstName = "Jean",
	lastName = "Dupont",
	country = "France",
	country = "Germany"
};
</cfscript>

Implicit Structures - Duplicate Keys

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.

In arithmetic and algebra, when a number or expression is both preceded and followed by a binary operation (a calculation involving two operands), a rule is required for which operation should be applied first. From the earliest use of mathematical notation, multiplication took precedence over addition, whichever side of a number it appeared on. Thus 3 + 4 × 5 = 5 × 4 + 3 = 23.

Operator precedence, therefore, determines the order in which operators are evaluated in a statement. Operators with higher precedence are evaluated first.

A simple example can be expressed as follows:

<cfscript>
x = 3 + 4 * 5;
writeOutput(x); //returns 23
</cfscript>

This is equivalent to wrapping the 4 * 5 in parentheses:

<cfscript>
x = 3 + (4 * 5);
writeOutput(x); //returns 23
</cfscript>

If we switched the parentheses, the result returned would be entirely different:

<cfscript>
x = (3 + 4) * 5;
writeOutput(x); //returns 35
</cfscript>

This is an important point to note. Operators have an order of precedence, but this can be overridden using parentheses. Statements within parentheses are always evaluated first, before moving on to the outer statements.

Associativity

The concept of Associativity determines the order in which operators of the same precedence are processed. For example, consider the following expression (where OP stands for order of precedence):

a OP b OP c

Left-associativity (left-to-right) means that it is processed as:

(a OP b) OP c

Right-associativity (right-to-left) means it is interpreted as:

a OP (b OP c)

The following table details operators order of precedence:

Precedence Operator type Associativity Individual operators
1 increment n/a ++
decrement n/a
2 logical-not right-to-left !
unary + right-to-left +
unary negation right-to-left -
3 multiplication left-to-right *
division left-to-right /
modulus left-to-right %
4 addition left-to-right +
subtraction left-to-right -
5 relational left-to-right <
<=
>
>=
6 equality left-to-right ==
!=
7 logical-and left-to-right &&
8 logical-or left-to-right ||
9 assignment right-to-left =
+=
-=
*=
/=
%=
&=

Summary

Warning: Multiplication and division are of equal precedence, and addition and subtraction are of equal precedence. Using any of the above rules in the order addition first, subtraction afterward would give the wrong answer to

10 - 3 + 2

The correct answer is 9, which is best understood by thinking of the problem as the sum of positive ten, negative three, and positive two.

10 + (-3) + 2

It is usual, wherever you need to calculate operations of equal precedence to work from left to right. The following rules of thumb are useful:

First: perform any calculations inside parentheses (brackets)

Second: Next perform all multiplication and division, working from left to right

Third: Lastly perform all addition and subtraction, working from left to right

However, with experience, the commutative law, associative law, and distributive law allow shortcuts. For example,

17 x 24 / 12

is much easier when worked from right to left, where here the answer is 34.

GeSHi started out as a module for the phpBB forum system to enable highlighting of programming languages, which was largely unavailable at the time. Since its conception it has spawned into a standalone project, supported by many web-based PHP content management systems.

Since I’m a ColdFusion developer, it was necessary to write a language reference file for ColdFusion and in particular, version 8.

You can download the file (30KB) here and upload it into your website’s GeSHi folder. You will need to rename it to cfm.php.

By including the content between a set of pre tags, <pre lang=”cfm”></pre>, GeSHi will implement the correct syntax for ColdFusion.

In addition to the comparison operators, which can be used on string values, the concatenation operator (&) concatenates two string values together, returning another string that is the union of the two operand strings. For example, “my ” & “string” returns the string “my string”.

The shorthand assignment operator &= can also be used to concatenate strings. For example, if the variable mystring has the value “alpha”, then the expression mystring &= “bet” evaluates to “alphabet” and assigns this value to mystring. This expression can be used in all CFML expressions.

<cfscript>
mystring = "Alpha";
mystring &= "bet";
</cfscript>
 
<cfdump var="#variables#" label="Concatenating Strings" />

This returns the following output:

Concatenating Strings Operator Example

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>

Following on from my posts on Arithmetic and Assignment operators, Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

The logical operators are described in the following table:

Operator Usage Description
Logical AND (&&) expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
Logical OR (||) expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.
Logical NOT (!) !expr Returns false if its single operand can be converted to true; otherwise, returns true.

Using ColdFusion, the Logical operators can be expressed as follows:

Logical AND

In the below example, both operands, x and y, return true, therefore myVariable is set to true within the logical statement.

<cfscript>
x = 9;
y = 2;
myVariable = false;
 
if (x < 10 && y > 1)
{
	myVariable = true;
}
</cfscript>

Logical AND Operator Example Results

Logical OR

In the below example, neither x equals 10 nor y equals 1, therefore the first part of the logical statement is not resolved. Instead, the else statement is processed, assigning the boolean value false to the variable myVariable.

<cfscript>
x = 9;
y = 2;
 
if (x == 10 || y == 1)
{
	myVariable = true;
}
else
{
	myVariable = false;
}
</cfscript>

Logical OR Operator Example Results

Logical NOT

In the below example, x does not equal y. Ordinarily this would result in the logical statement being evaluated, however, a logical negation has been applied to the beginning of the expression to be evaluated, therefore the variable myVariable is assigned the boolean value true.

<cfscript>
x = 9;
y = 2;
myVariable = false;
 
if (!x==y)
{
	myVariable = true;
}
</cfscript>

Logical NOT Operator Example Results

In my first post in this series, I introduced Using JavaScript Arithmetic Operators in ColdFusion 8. Now we’ll concentrate on Assignment operators.

An assignment operator assigns a value to its left operand based on the value of its right operand.

The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x. The other assignment operators are usually shorthand for standard operations, as shown in the following table.

Operator Shorthand operator Meaning
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y

In unusual situations, the assignment operator is not identical to the Meaning expression in the above table. When the left operand of an assignment operator itself contains an assignment operator, the left operand is evaluated only once. For example:

<cfscript>
a[i++] += 5 //i is evaluated only once
a[i++] = a[i++] + 5 //i is evaluated twice
</cfscript>

« Older entries § Newer entries »