Learning ColdFusion 9: Referencing Arrays Returned From Methods

Posted July 14, 2009 at 1:51 PM by Ben Nadel

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.


You Might Also Be Interested In:



Reader Comments

Jul 14, 2009 at 2:25 PM // reply »
33 Comments

component[ methodName ]() would be great.


Jul 14, 2009 at 2:32 PM // reply »
11,238 Comments

@Adam,

Yeah, it would be double super awesome.


Jul 14, 2009 at 2:36 PM // reply »
67 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.


Jul 14, 2009 at 2:46 PM // reply »
33 Comments

@Rick lol! Fair enough. Though I like the .NET platform, that's still funny.


Jul 14, 2009 at 2:58 PM // reply »
26 Comments

I really like this addition.


Jul 14, 2009 at 3:09 PM // reply »
26 Comments

And it's about time, too.


Jul 14, 2009 at 3:33 PM // reply »
170 Comments

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 [];
}

#get()[1]#

This would throw an error because the array is empty.


Jul 14, 2009 at 3:36 PM // reply »
11,238 Comments

@Dan,

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.


Jul 14, 2009 at 4:07 PM // reply »
35 Comments

I second (third?) Adam and Ben.

component[ methodName ]() would be great.


Jul 14, 2009 at 10:12 PM // reply »
53 Comments

I thought [] was for arrays, wouldn't it be:

component{methodName}()?


Jul 15, 2009 at 7:56 AM // reply »
11,238 Comments

@Andrew,

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

array[ index ]

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


Jul 15, 2009 at 6:28 PM // reply »
53 Comments

@Ben

Cool, learn something new everyday.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools