Learning ColdFusion 9: Referencing Arrays Returned From Methods

Posted July 14, 2009 at 1:51 PM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page


You Might Also Be Interested In:




Reader Comments

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

component[ methodName ]() would be great.


Jul 14, 2009 at 2:32 PM // reply »
7,572 Comments

@Adam,

Yeah, it would be double super awesome.


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

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


Jul 14, 2009 at 2:58 PM // reply »
21 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 »
124 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 »
7,572 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 »
51 Comments

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

component{methodName}()?


Jul 15, 2009 at 7:56 AM // reply »
7,572 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 »
51 Comments

@Ben

Cool, learn something new everyday.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 22, 2010 at 7:43 AM
Terms Of Service / Privacy Policy Document Generator
Thankyou for this very helpful form. You've made my life much easier today. I'll have a look around your site... I'm sure there's some more good stuff here..Thanks Dave ... read »
Mar 22, 2010 at 7:21 AM
Encountered "(. Incorrect Select Statement, Expecting a 'FROM', But Encountered '(' Instead, A Select Statement Should Have a 'FROM' Construct.
I got this exception now. In case you're using var-es local struct, CF gives you couple of "new" exceptions: Encountered "local. and Encountered "id. Incorrect Select List, Incorrect select colum ... read »
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »