I admit it. When I was a young programmer Arrays confused the heck out of me so I coded around them whenever possible. As the decades passed, I finally came to understand and even like using them...with one caveat. Creating an array is a pain. It took more code to create them than seemed necessary. Notice I used past tense there? Adobe has made it much easier to create arrays AND structures. Why is this cool? Because it cuts down on the coding needed by a ton, and is more in-sync with a lot of other languages out there! Check this out....
This is the old way to create a one-dimensional array. Yes, there are two and three-dimensional arrays but I am not going to get into that here.
This code creates an array object that looks like so when dumped to the screen:
CFDUMPtag to display ColdFusion objects and elements when debugging your code. For example, want to see what data is being passed when you submit a form? <cfdump var="#form#"> will do that. Need to see what application variables are set in your app? <cfdump var="#application#"> will give you those. Need to see the data in a session? <cfdump var="#session#"> will do that for you. It really is a useful tag and one thing you should know...you can dump and abort at the same time like this <cfdump var="#oldArray#" abort>. Yep, it's that easy.
Using array syntax we can output specific values like so:
Which will display the value....
Now, this isn't new or anything. If you have ever created an array in ColdFusion then this should look at least a little familiar. A one-dimensional array like this is pretty easy to understand and access in CFML, but once you get into multi-dimensional arrays or arrays of arrays it gets much harder and more annoying to use. Not to mention the code itself is verbose.
Flash forward to 2014 when Adobe released ColdFusion 11. They included a way to create arrays in what is called literal syntax. At first it may seem confusing but once you do it a few times it becomes clear that this is a much easier way to create arrays.
You can see the above code, the first line creates an array with the same number of values that it took six lines to create using the old method. Output and dumping the object are, of course, done the same way.
You can see the basic syntax of creating an array this way is
Now, suppose we have more data and want to assign it to an array containing arrays. Why would we do this? Eh, we probably wouldn't unless we are trying to use external data or organize a mess into something useful. See here where I create a series of arrays about a certain TV show for each of the five series.
Star Trek 1966-1969 Captain Kirk
As you can see it's basically
arrayName = [["array 1 item 1","array 1 item 2"],["array 2 item 1","array 2 item 2"]]. For some reason it is much easier to keep track of than tons of lines of CFSET tags.
Creating structures in ColdFusion has always been a bit easier than arrays. Remember that structures are assigned to a key. Let's say you create a structure named USER and you want to assign a person's username to it. You would set USERS.USERNAME equal to the username, etc. Overall structures are way easier because we use similar syntax when scoping variables all day long. So USERS.USERNAME is the same basic syntax as URL.PAGENUMBER or CGI.REMOTE_HOST. Make sense?
When should you use an array v.s. a structure? Think of arrays as a spreadsheet of data stored in memory. Think of structures as one row of that spreadsheet stored in memory. Structures are almost like storing a form submission in memory and using the data whenever you like.
In the past, I have used arrays to organize messy data into a clean structure. I recall having several comma separated lists of courses for half a dozen instructors - so I created an array that contained the instructors and then an array inside each that contained their courses.
Structures come in handy when you need to retain some info for a user. Say you have a form and the user fills it in and then submits - but they screwed something up and have to go back. Normally they would need to fill the form in again. Instead you create a structure in the session scope with blank values, then fill in the form with those values by default. When the user submits their data you update the structure in the session with their info and then when you send them back the form is pre-populated with their previous info.
Of course, ask me if you need the code for either of these.
Okay, back to the topic at hand. Creating a structure the old way was pretty easy - yet still pretty hefty in lines of code.
On Star Trek the Next Generation the Captain is Picard.
Again, fairly verbose. Creating structures with literal syntax is almost the same as creating an array.
You can create a structure that contains structures in CF. Heck, you can create structures that contain arrays, arrays that contain structures, arrays that contain arrays that contain structures...and on and on. My tip is to keep it simple. Simple is always the best solution.
Here's how we would create a structure that contains structures.
"Time's a wasting.", said Archer
Okay let's see if we can blow your minds by combining arrays and structures. Let's say we wanted to store a ton of info in a relatively structured collection. I am going to use a structure for that since they have unique keys. This will allow me to use the key name instead of array element positions like , etc. Because we need to collect the data that will be populating the main structure we need to create this in reverse order.
First I need to create an array of cast members for each show. Without this we can't populate the seriesData structure.
Next we will create the seriesData structure that, you will notice, contains the array of cast members we just created.
Now for good measure we will add another series to the data so we can see how more than one collection of data will appear.
Now we populate a structure with the created elements.
Let's see how disgusting this thing looks and how we can extract data from it.
Yeah, that will hurt your brain. I know, I said keep it simple, but understanding the complex way helps the simple stuff seem easier! Digging into this data, if we want to access the title from the first structure key we would use
#seriesInfo.TOS.title#. To access the array of cast members we need to dig deeper with
#seriesInfo.TOS.cast#, or to get the first actor and character we would use
Since we know where that array of cast members is we can actually loop over it. Check this out.Star Trek the Original Series aired from 1966-1969
Again, I don't condone using this super complicated method unless you have no alternative. More than likely you would have to use this when given data from another source.
I am sure this is probably clear as mud...but using the basic syntax below should allow you to create snippets so you can juist squirt the code into your apps and make it work for you.
ColdFusion Tip: Did you know you can use array syntax with query output? If you have query named PEOPLE containing columns FNAME, LNAME, EMAIL and wanted to get the first email address in the recordset you would use the following syntax: