[thelist] ColdFusion For/Next Loops (was: SQL Assistance)

Jeff Howden jeff at jeffhowden.com
Tue Apr 1 09:57:20 CST 2003


morgan,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Morgan Kelsey
>
> when performing a to= from= loop using the cfloop tag,
> cf processes the "from" attribute at each iteration.
>
> That means, if you have an array of 50 items, MyArray,
> executing:
>
> <cfloop from="1" to="#ArrayLen(MyArray)#" index="i">
> <!--- process code here --->
> </cfloop>
>
> will process the ArrayLen function everytime around
> the loop!
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

i know we both got this info from a trusted, reliable source (ie, macromedia
employee), but i just wrote up a code sample that suggests that doesn't
actually happen.  try this out and see what i mean:

<cfset myArray = ArrayNew(1)>
<cfloop from="1" to="10" index="i">
  <cfset myArray[i] = i>
</cfloop>

<cfloop from="1" to="#ArrayLen(myArray)#" index="j">
  <cfset myArray[ArrayLen(myArray) + 1] = myArray[j] + j>
  <cfoutput>#j#,</cfoutput>
  <cfif j EQ 100>
    <cfbreak>
  </cfif>
</cfloop>

<cfoutput>
#ArrayLen(myArray)#
</cfoutput>

i tested this on both cf5 and cfmx and get the following output:

1,2,3,4,5,6,7,8,9,10,20

if it actually ran the ArrayLen() function in the to attribute every time
then logic would dictate we'd have a comma-delimited list from 1 to 90 and
the final ArrayLen() would output 100.

to see what i'm talking about, try this block of javascript, an example
where the conditional is executed each iteration of the loop:

<script language="JavaScript" type="text/javascript">
<!--
  myArray = new Array();
  for(var i = 0; i < 10; i++)
    myArray[i] = i;

  for(var j = 0; j < myArray.length; j++)
  {
    myArray[myArray.length] = myArray[j] + j;
    document.write(j + ',');
    if(myArray.length == 100)
      break;
  }
  document.write(myArray.length);
//-->
</script>

the output from this script block is:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,2
9,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
80,81,82,83,84,85,86,87,88,89,90,100

just for sh!ts and g!ggles i wrote up the same block of code, but in a
<cfscript></cfscript> block.  surprisingly, and counter to what we both
heard (or thought we heard), the output is the same as the javascript block
above meaning the condition of the loop is evaluated each iteration.  for
your enjoyment, here's that block of code:

<cfscript>
  myArray = ArrayNew(1);
  for(i = 1; i LTE 10; i = i + 1)
    myArray[i] = i;

  for(j = 1; j LTE ArrayLen(myArray); j = j + 1)
  {
    myArray[ArrayLen(myArray) + 1] = myArray[j] + j;
    writeoutput(j & ',');
    if(ArrayLen(myArray) EQ 100)
      break;
  }
  writeoutput(ArrayLen(myArray));
</cfscript>

enjoy,

.jeff

http://evolt.org/

NOTICE:  members.evolt.org web and email address are changing!
---------------------------------------------------------------------
| OLD:                            | NEW:                            |
| jeff at members.evolt.org          | evolt at jeffhowden.com            |
| http://members.evolt.org/jeff/  | http://evolt.jeffhowden.com/    |
---------------------------------------------------------------------





More information about the thelist mailing list