Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 798
Phillip Senn Avatar asked Jan 07 '10 14:01

Phillip Senn


2 Answers

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>
like image 51
Lostlinkpr Avatar answered Oct 05 '22 22:10

Lostlinkpr


<cfscript>
  var i = 0;
  var l = ListLen(qry.Columnlist);
  var FieldName = "";
  for (i = 1; i lte l; i = i + 1)  // you also can use i++ instead
  {
    FieldName = ListGetAt(qry.Columnlist, i);
    form[FieldName] = qry[FieldName][1];
  }
</cfscript>

EDIT Nicer (maybe a even little faster, for really heavy loops) version of the above:

<cfscript>
  var i = 0;
  var Fields = ListToArray(qry.Columnlist);
  var FieldName = "";
  var l = arrayLen(Fields);
  for (i = 1; i lte l; i = i + 1)  // you also can use i++ instead
  {
    FieldName = Fields[i];
    form[FieldName] = qry[FieldName][1];
  }
</cfscript>
like image 34
Tomalak Avatar answered Oct 06 '22 00:10

Tomalak