Beginners Guide To CF: Complex Objects - Part 1
One of the best things about ColdFusion is that it is easy to learn. One of the worst things about ColdFusion is that it is easy to learn. Let me qualify each of those statements with a quick explanation.
Why is the ease of ColdFusion a good thing? Because it allows seasoned developers to make a quick adjustment to a new language and create extremely powerful dynamic applications with an extremely low learning curve.
Why is the ease of ColdFusion a bad thing? Because it allows people with absolutely no programming background to create dynamic applications. So how is that a bad thing? I can speak to this first hand. When I first started with CF, I had no programming background (unless you consider hacking around on my Commodore 64 when I was 8 years old a "background"). I didn't know HTML or SQL. I never took any formal classes on programming so I didn't really comprehend some of the basics that are necessary to fully leverage the power of CF. I've since grown a lot in my comprehension of programming concepts and theories, but I think there are a lot of developers who may struggling with some of the basics that are preventing them from growing and learning. I hope to break down some of these barriers for these developers by focusing on some very basic concepts as I started to do with my post on CFCs. I'm going to try to provide clarity to one of the issues that I think is a basic foundation for advanced comprehension of CF: Complex Objects (Lists, Arrays, Structures and Queries). Originally I had planned this to be a single post, but due to the size I think I will break it up into a few parts. Think of these objects as 'containers' that hold data in different formats, each serving an individual purpose. These 'containers' can be used on their own but can also be placed inside of one another.
An essential tool in understanding complex objects is the 'cfdump' tag. Learn to use this tag religiously - it will greatly increase your comprehension of complex objects.
Lists
Important Links:
Adobe Livedocs - List Functions
Lists are not a 'complex object' by definition, but they certainly are a 'container' and act as such. I won't spend a lot of time on lists, because I think they are a pretty easy concept to grasp. However, I think it's definitely worth pointing out that many data elements can be treated as a list. Lists are essentially a collection of items separated by a common delimiter. Delimiters are often commas, but don't limit yourself into thinking this way. Delimiters can be dashes, slashes, pipes, etc. This sentence is a space delimited list. Lists can be iterated over (with a loop), manipulated (using listAppend, listPrepend, listSort, etc), searched (listFind, listFindNoCase, listContains, listContainsNoCase, etc), and converted to other Data Types (listToArray). Lists can not contain other complex objects, but other complex objects could potentially contain a list.
Sample List Code:
<!--- To reference a list, simply use cfoutput --->
<cfoutput>
#theList#
</cfoutput>
<!--- To reference a particular list item, use listGetAt --->
<cfoutput>
The third item in the list is #listGetAt(theList, 3, ",")#
</cfoutput>
Structures
Important Links:
Adobe Livedocs - Structure Functions Adobe Livedocs - About Structures
Like lists, structures (also known as 'structs') are also pretty simple 'containers'. They are essentialy just groups of common variables all tied together by a single name. For example, session variables are all contained in a structure. Structures always are organized in key-value pairs. What is a key-value pair? That just means that the left column (or key) always has an assosiated right column (or value). Unlike arrays, the structure 'key' must be a string. The easiest way to comprehend a structure is by dumping it and examining it.
Creating a struct is pretty easy.
<cfset theStruct = structNew()>
Setting elements of a struct is also easy. Simply use another cfset to add a key (using dot notation) and set the value. (Of course the value could easily be a dynamic variable instead of a hardcoded value).
<cfset theStruct.theSecondElement = "number 2">
<!--- dump the struct --->
<cfdump var="#theStruct#">
Dumping the struct will give a simple visual representation.

Structures can and will become more complex. Structures can contain other complex objects like queries, arrays (though it's more common to see an arrayOfStructs) or even additional structures (known as a structOfStructs) within a given key.
The easiest way to reference a struct is to use dot notation. You can not directly reference the name of a struct within cfoutput - doing so will throw an error (Complex object types cannot be converted to simple values).
You can NOT reference a struct
directly within a cfoutput like this:
<cfoutput>
#theStruct#
</cfoutput>
however you can dump the entire structure
--->
<!--- Reference a structure using dot notation --->
<cfoutput>
The first value in the struct is #theStruct.theFirstElement#
</cfoutput>
<!---
Reference structures with keys containing
forbidden characters (numbers, spaces, etc) using
the associative array (bracket) notation
(quotes are not needed if the key is a variable)
--->
<cfoutput>
The first value in the struct is #theStruct["theFirstElement"]#
</cfoutput>
I think Ill end here for now. My next post will focus on arrays and queries.






There are no comments for this entry.
[Add Comment]