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  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

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

component[ methodName ]() would be great.


Jul 14, 2009 at 2:32 PM // reply »
6,515 Comments

@Adam,

Yeah, it would be double super awesome.


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

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


Jul 14, 2009 at 2:58 PM // reply »
15 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 »
111 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 »
6,515 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 »
45 Comments

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

component{methodName}()?


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

@Ben

Cool, learn something new everyday.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »
Nov 20, 2009 at 5:07 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, I have seen tidbits about the way Railo handles session. I can understand that it lazy-loads sessions, but I also think that I might make some things more complicated. For example, often tim ... read »
Nov 20, 2009 at 4:53 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, you can ramp up the security by turning on J2EE session which gives you a third set of numbers other than CFID/CFTOKEN. There's a reason why ACF put this in place (other than just session replic ... read »
Nov 20, 2009 at 4:52 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Case in point, Ben, you may not be aware of this, but in Railo - OnApplicationStart() & OnSessionStart() act differently than in ACF. ACF does: OnApplicationStart (1st hit) OnSessionStart (1st and e ... read »
Nov 20, 2009 at 4:46 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, That's understandable. I am not sure if this really leaves any more security holes than the fact that using old cookie-based CFID / CFTOKEN values will create a new session using the old CFI ... read »