Thursday, January 7, 2010

All I wanted for Christmas was a CFSCRIPT FOR .. IN LOOP (For Arrays)

Okay, so Christmas is past and CF9 is long out of the beta stage. But my tiny wish remains. The reasonable part of me says, "It is really not that hard to construct an alternative". Especially with all the enhancements in CF8 and CF9. It comes much closer to perfection than in any previous versions.


<cfscript>
for (x = 1; x <= arrayLen(myArray); x++)
{
WriteOutput(myArray[x] &"<hr />");
}
</cfscript>

But there is something classic and elegant about a for in loop. Simple, intuitive and just downright beautiful code. If it were any more beautiful, I just might break down and cry. {Sigh} If only it worked. If only ...
<cfscript>
for (x in myArray)
{
WriteOutput(x &"<hr />");
}
</cfscript>

<childishWhine>
All the other languages have it? Why can't we?
</childishWhine>

;)

8 comments:

Unknown January 7, 2010 at 3:07 PM  

Specially since it is already in the cfloop tag

cfSearching January 7, 2010 at 3:28 PM  

@raulriera,

Yes, the lack of symmetry irks me. It disturbs the perfect order of the CF9 universe ;-)

-Leigh

Anonymous,  January 7, 2010 at 10:07 PM  

why can't CFSCRIPT be Python... oh... wait

Headsplode January 8, 2010 at 5:07 AM  

It isn't for/in but you can use while with an iterator.

http://blog.mxunit.org/2009/10/looping-over-array-in-cfscript.html

Unknown January 8, 2010 at 6:15 AM  

i feel your pain. i've always said, either make the cfscript syntax equal to tag syntax in everyway or just get rid of it.

there are so many things missing from cfscript that it's just worthless.

cfSearching January 8, 2010 at 12:22 PM  

(I think the blogger ate my homework .. err.. last response. So this may be a duplicate)

@tpetruzzi - I have not done a complete comparison, but at least CF9 is _much_ better in that area. It fills in a lot of the gaps that existed in previous versions. Though I have no idea why they would chose to omit support for array loops ...


@Travis,

Yes, using java iterators is a really slick trick! It probably the next best thing. However, though I truly love the java stuff - I am admittedly a coward about mixing CF and the undocumented java features. (Perhaps unnecessarily so) I am always afraid they will change the underlying classes and break my code. (I got burned by that once before.)

Though to be fair, it seems very unlikely that arrays will change to something other than a java.util.List in the near future. Realistically, what else would they use ? ;)

-Leigh

EECOLOR June 8, 2010 at 4:35 AM  

"I am admittedly a coward about mixing CF and the undocumented java features. (Perhaps unnecessarily so)"

The underlying code implements java.util.Collection. This is the most basic collection interface. This means that it is highly unlikely that it will change. This also means that it will probably work in Railo too.

Greetz Erik

cfSearching June 10, 2010 at 1:00 AM  

@Erik,

It is not the underlying java classes/interfaces I am concerned with, but CF's _implementation_ of its internal internal classes. However unlikely it may seem, there is always the chance CF will change how it implements something. Then any code that depends on it will break.

Granted, lists (or CF arrays) are pretty fundamental. Not as much as Strings, but I too would be surprised if CF's implementation changed any time soon. Still ... having gotten burned once before, I am overly cautious now ;)

-Leigh

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep