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,246 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 »
171 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,246 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,246 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 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools