ColdFusion Programming/data structures

From Wikibooks, open books for an open world
Jump to: navigation, search


The truth is that in Coldfusion a list is nothing more than a string separated by a delimiter. The default delimiter is a comma. Any delimiter can be used though.For example, ColdFusion Lists:ColdFusion Arrays:ColdFusion Structures is no less a list than the comma delimited list.

Creating a list

<cfset newlist = "IL,MO,IA,MN">

Output: IL,MO,IA,MN

Changing a list


Note that these functions do not change the original list but rather their return value is the modified list that must be set to a variable which can be the same one that you began with.

<cfset newlist = listappend(newlist, "IN")>

Output: IL,MO,IA,MN,IN


<cfoutput>#ListGetAt(newlist, 4)#</cfoutput>

Output: MN



Output: 5


<cfset newlist = ListSetAt(newlist, 3, "HI")>

Output: IL,MO,HI,MN,IN


<cfset newlist = ListInsertAt(newlist, 3, "AL")>



<cfset newlist = ListDeleteAt(newlist, 4)>

Output: IL,MO,AL,MN,IN


<cfset newlist = ListSort(newlist, text)>

Output: AL,IL,IN,MN,MO


ColdFusion Arrays are very different than lists and provide many more utilities for manipulation. Arrays are collections of objects all linked together front-to-back.

To create an array, do the following:

<cfset myArray = ArrayNew(1)>

There are many functions to manipulate Arrays. For instance, to add an item to myArray you may use the function ArrayAppend().

<cfset ArrayAppend(myArray(1), "my first array item")>

Multi-dimensional Arrays[edit]


ColdFusion structures are similar in many regards to other programming languages.

<cfset picnic = StructNew()>
<cfset picnic.fruits = "apples, oranges, pears">
<cfset picnic.drinks = "wine, water">


In ColdFusion Queries are a datatype.

<cfquery datasource="mydatabase" name="myDataQuery">


When working with structures one of the most powerful tools available to you to help debug and solve programming issues is the Cfdump tag. The Cfdump tag allows you to visually display any variable regardless of its structure type even when you have nested structures.

<cfdump var="#mystruct#" expand="yes">

where mystruct is the name of the variable to be displayed. Take note that mystruct is surrounded by pound signs. This tag does not take the name of the variable but rather the variable itself. So if we create code like:

<cfdump var="mystruct" expand="yes">

the output would be the string: mystruct not the values within mystruct.