Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Andy Bellenie
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Andy Bellenie@andybellenie )

Learning ColdFusion 9: Referencing Arrays Returned From Methods

By Ben Nadel on
Tags: ColdFusion

This is a really minor syntax update in ColdFusion 9, but again, one that will probably remove a good amount of friction in certain areas. In ColdFusion 9, if you perform a method call that returns an array, you can now reference the array index directly off of the method call result without creating an intermediary variable. Meaning, you can now do things like this:

someMethod()[ 1 ]

Assuming that "someMethod" returns an array, the statement above will return the first element in the resultant array.

While this might seem like a rare scenario, it actually comes up all the time when using methods like REMatch() and XmlSearch(). To see this in action, let's take a look at an XmlSearch() example:

  • <!--- Define an XML document. --->
  • <cfxml variable="girls">
  • <girls>
  • <girl>
  • <name>Tricia</name>
  • </girl>
  • </girls>
  • </cfxml>
  • <!---
  • Search for the name node and then get a reference to
  • the first one returned in the results.
  • --->
  • <cfset nameNode = xmlSearch( girls, "//name" )[ 1 ] />
  • <!--- Output name value. --->
  • Name: #nameNode.xmlText#

Here, we are searching for the "name" node using XmlSearch(). XmlSearch() will return an array of all the nodes matching the given XPath query. We then take that resultant and, without an intermediary variable, grab the first node. Running the code above, we get the following output:

Name: Tricia

This is quite minor in scope, but very huge in payoff! Like I said with the implicit array / struct usage and the ternary operator, it's the small updates that can really have a profound effect over codability in the long run.

Technically, I don't think this is really an upgrade to the language - it's an update to the compiler (are these different things)? I say this because perviously, all the ingredients were already there: methods returned arrays and arrays could be directly accessed. The problem was that before ColdFusion 9 came along, this aspect of the code simply wouldn't compile:


Now that this combination of characters does compile, the functionality is naturally there.

Unfortunately, the reverse of this is not yet available:


If this could compile, then we'd be able to call methods on array indices as in:

component[ methodName ]()

Trying to do this will still throw the ColdFusion compile-time error:

Invalid CFML construct found on line X at column Y. ColdFusion was looking at the following text: (

Maybe they will rock that update for the final ColdFusion 9 release. While still a minor change, it would be quite major in terms of dynamic functionality.

Looking For A New Job?

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments

Not that you would ever do anything like this, of course. Because, you know, every time you dereference a return value without error checking, God kills a kitten. With an ASP.NET reference manual.

As Rick pointed out, you just have to be careful w/this syntax because if your function does not return an item at the position you're reference, errors will be thrown.

For example:

function get(){
return [];


This would throw an error because the array is empty.


I thought about that when I was writing this up. But, then I figured if you were going to be confident enough to directly reference a result, then you probably:

A) Are working with a situation that will return a result.

B) Have some sort of error handling set up to deal with an unexpected, "critical" error.

Of course, I probably should have mentioned it, nonetheless.


Square bracket notation is used for both array-index reference and *can* be used for struct-key reference:

array[ index ]

struct[ "key" ] === struct.key