ColdFusion Programming/data structures

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

Lists[edit]

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">
<cfoutput>#newlist#</cfoutput>

Output: IL,MO,IA,MN

Changing a list

LISTAPPEND

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")>
<cfoutput>#newlist#</cfoutput>

Output: IL,MO,IA,MN,IN

LISTGETAT

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

Output: MN

LISTLEN

<cfoutput>#ListLen(newlist)#</cfoutput>

Output: 5

LISTSETAT

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

Output: IL,MO,HI,MN,IN

LISTINSERTAT

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

Output: IL,MO,AL,HI,MN,IN

LISTDELETEAT

<cfset newlist = ListDeleteAt(newlist, 4)>
<cfoutput>#newlist#</cfoutput>

Output: IL,MO,AL,MN,IN

LISTSORT

<cfset newlist = ListSort(newlist, text)>
<cfoutput>#newlist#</cfoutput>

Output: AL,IL,IN,MN,MO

Arrays[edit]

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]

Structures[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">

Queries[edit]

In ColdFusion Queries are a datatype.


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

CFDUMP[edit]

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.