Thursday, July 23, 2009

Looping in ColdFusion

I really wish you could loop an array, query and struct in a consistent manner in ColdFusion. In truth, you're just iterating over a collection of data. Why does it need to be so different? For example, here's looping over a collection of Users and retrieving a single User record.


<cfloop array="#users#" index="user">

</cfloop>



<cfloop collection="#users#" item="i">
<cfset user = users[i] />
</cfloop>



<cfloop query="users">
<!--- convert the row into a struct in order to access all the user's data in a single collection --->
<cfset user = {} />
<cfloop list="#users.columnList#" index="i">
<cfset user[i] = users[i][currentRow] />
</cfloop>
</cfloop>


I really wish I could do...


<cfloop collection="#users#" index="user">

</cfloop>



<cfloop query="#users#" index="user">

</cfloop>


... but that might make too much sense.

Seriously, someone just needs to take all the nice parts of ColdFusion and re-write the damn language. It would make me so much happier. Also, don't get me started on the lack of #'s in cfloop query.

3 comments:

  1. Switch to railio, they were able to go from the ground up and is so much faster, but I would blame the CFML folks that decide how the language should work

    ReplyDelete
  2. Also, I want to be able to loop using data="#users#" index="user" without having to specify array/collection/query since ColdFusion should be able to figure how how to handle it.

    ReplyDelete
  3. Waiting for For...in loop array support in CFScript in CF9.0.1

    ReplyDelete