Looping over lists in cf9

Phillip Senn picture Phillip Senn · Jan 7, 2010 · Viewed 15.1k times · Source

Page 116 of the developer's guide says "Unlike the cfloop tag, CFScript for-in loops do not provide built-in support for looping over queries and lists."

Q: How do I loop over a list using the new script syntax in ColdFusion 9?

<cfloop list="#qry.Columnlist#" index="FieldName">
   <cfset form[FieldName] = qry[FieldName][1]>
</cfloop>

Answer

Lostlinkpr picture Lostlinkpr · Mar 29, 2011

You can also try the listToArray and then use the for-in construct for Arrays in CF9 as:

<cfscript>
aCol = listToArray (qry.ColumnList);

for( fieldName in aCol ){
    form[fieldName]  = qry[fieldName][1];
}

</cfscript>