ColdFusion Programming/data structures
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>
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")> <cfoutput>#newlist#</cfoutput>
<cfset newlist = ListSetAt(newlist, 3, "HI")> <cfoutput>#newlist#</cfoutput>
<cfset newlist = ListInsertAt(newlist, 3, "AL")> <cfoutput>#newlist#</cfoutput>
<cfset newlist = ListDeleteAt(newlist, 4)> <cfoutput>#newlist#</cfoutput>
<cfset newlist = ListSort(newlist, text)> <cfoutput>#newlist#</cfoutput>
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")>
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.