Article
Introduction to Data Structures in ColdFusion
I know that this is everyone's favorite topic. It's certainly the first chapter I look for when I get a new Learn <Insert Language Here> in 2 Minutes book.
Well, ok -- maybe not. But the gruesome truth is that data structures are at the core of our very existence as developers. Whether you're building a shell script to run on a *nix box or a full-fledged Windows desktop application, you'll need data structures of some type.
Anyone who's even briefly looked at ColdFusion has probably seen the most basic data structures, variables. Variables are generally set like this:
<cfset myVariable = \\"This is my variable.\\">
What many people have not experienced is the power of ColdFusion's more advanced data structures. That's okay, though, because in this article and the next, I'm going to introduce you to ColdFusion arrays and structures (or "structs", as they're called in CF).
Arrays
We'll begin by discussing how ColdFusion handles arrays. If you've developed software in any other language, you're no doubt familiar with arrays and their various uses. If not, that's okay -- I'll explain them here.
An array is simply a container that holds several variables with the same name. For example, let's say you have a bowl of fruit and you want to create a variable for each fruit. You start with the apple:
<cfset fruit = \\"apple\\">
Now, you've got a fruit that is an apple. Great! You move on to the orange:
<cfset fruit = \\"orange\\">
Wait! Now you've got one fruit that is an orange, but no apples. This is because with the second <cfset> you overwrote "apple" with "orange". So, how can we describe all of our fruit, but still keep it classified in the same category: fruit? Well, we use an array for this:
<cfset fruit = ArrayNew(1)>
This creates an array named fruit. It uses the function ArrayNew, which takes a single parameter that must be an integer. For those of you who are familiar with other programming languages (like Visual Basic, for example), this parameter is deceptive. The parameter here is not defining how many elements your array will contain, but how many dimensions it will have. Before we talk any more about dimensions, let's continue with a single dimension array. Now that we've created this one, we can add our fruit to it:
<cfset fruit[1] = \\"apple\\">
<cfset fruit[2] = \\"orange\\">
<cfset fruit[3] = \\"banana\\">
Perfect! Now we've listed our entire bowl of fruit in a single variable name, so we can easily keep track of it. Let's examine this a little further. First of all, in ColdFusion arrays are all dynamic. This means that ColdFusion does not require you to tell it how many elements your array will have in advance (other languages, like Java, require this). Second of all, the array indexing begins at 1, not 0. Languages like Java and C++ would begin the above array by setting fruit[0] = "apple".
Now, let's talk about displaying an array. It's really very simple. We'll use a <cfloop> and the ArrayLen function.
<cfloop from=\\"1\\" to=\\"#ArrayLen(fruit)#\\" index=\\"i\\">
<cfoutput>#i# = #fruit[i]#<br></cfoutput>
</cfloop>
That's not so hard, is it? Those three lines of code simply dump out the contents of the array, one element per line. Notice how you access each element, using the i at the end of the variable name. What's happening here is we're starting our loop at 1, assigning that value to the variable i. We then display the position we're currently at, and then access fruit[1] and display it. Next, it increments i to 2, displays the variables, and so on until i is equal to the length of the array.
For all you Java/C++ coders, we're looping to the actual length of the array, not length minus one. In Java, this would throw an array out of bounds exception because indexing starts at 0 and goes to length minus one.
David operates