Search code examples
coldfusioncoldfusion-9cfloop

Looping over lists in cf9


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>

Solution

  • 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>