Looping over arrays in Lucee CFML

I’ve been back to doing some coding in CFML this week and working on a project that has been worked on by several different people and I happened to notice several different ways had been used to loop over arrays in various different parts of the code, one of which I must admit I had never actually seen before. I thought I would look into all the ways of looping over arrays in Lucee CFML and see how many I could find.

Lets start with the “traditional” way:

<cfset myArray = ['a','b','c','d','e']>
<cfloop from="1" to="#arrayLen(myArray)#" index="loopIndex">
    <cfoutput>#loopIndex# - #myArray[loopIndex]#<br></cfoutput>
</cfloop>

This is how I would have pretty much always have looped over an array in tags and if it was in cfscript it would have been:

<cfscript>
    myArray = ['a','b','c','d','e'];
    for (i = 1; i <= arrayLen(myArray); i++) {
        echo("#i# - #myArray[i]#<br>");
    }
</cfscript>

You can also use the tags-in-script version:

<cfscript>
    myArray = ['a','b','c','d','e'];
    loop from="1" to="#arrayLen(myArray)#" index="loopIndex" {
        echo("#loopIndex# - #myArray[loopIndex]#<br>");
    }
</cfscript>

You can also use a “for” loop:

<cfscript>
    myArray = ['a','b','c','d','e'];
    for (i = 1; i <= arrayLen(myArray); i++) {
        echo("#i# - #myArray[i]#<br>");
    }
</cfscript>

Now, here is the version I hadn’t seen before, using the array, item and index attributes with the cfloop tag:

<cfset myArray = ['a','b','c','d','e']>
<cfloop array="#myArray#" item="loopItem" index="loopIndex">
    <cfoutput>#loopIndex# - #loopItem#<br></cfoutput>
</cfloop>

You can also use the tags-in-script version of this:

<cfscript>
    myArray = ['a','b','c','d','e'];
    loop array="#myArray#" item="loopItem" index="loopIndex" {
        echo("#loopIndex# - #loopItem#<br>");
    }
 </cfscript>

Then there is the for-in version:

<cfscript>
    myArray = ['a','b','c','d','e'];
    for (loopItem in myArray) { 
        echo("#loopItem#<br>"); 
    } 
</cfscript>

Also, there is the arrayEach function:

<cfscript>
    myArray = ['a','b','c','d','e'];
    arrayEach(myArray, function(loopItem,loopIndex) {
        echo("#loopIndex# - #loopItem#<br>"); 
    });
</cfscript>

Finally, you can also use the array each member function:

<cfscript>
    myArray = ['a','b','c','d','e'];
    myArray.each(function(loopItem, loopIndex) { 
        echo("#loopIndex# - #loopItem#<br>"); 
    });
</cfscript>

So, that is nine different ways to do exactly the same thing, looping over arrays in Lucee CFML.

I’m not sure we really need so many different ways to achieve the same thing, and having some many different options just means that sometimes, when multiple different people work on a project over many years, you end up with a codebase like I’ve worked on this week, with different ways of doing the same thing used, simply due to people’s personal preferences. Maybe there are some other ways that I’ve missed, but nine is more than enough!!!

3 Replies to “Looping over arrays in Lucee CFML”

  1. Richard Herbert November 3, 2018 at 12:07

    I’d be careful using arrayLen(myArray) to test for the end of the loop as you have in the first 4 examples. The value is evaluated on every iteration so there is a chance that the length of the array could have changed by the time the code loops around again. Better to capture the length of the array before the loop starts and use that value in the test.

    Reply

    1. Interesting point. I’ve not tested to see how Lucee handles you modifying the array in the loop to see if it re-evaluates the value in the “to” attribute or if it simply uses the value it gets at the start of the loop. If it does re-evaluate, then, depending on your use case, this may or may not be desired.

      Reply

  2. An old post, but I agree with Richard on this. In my early coding days (lots of PowerBuilder), I was heavily advised against putting any functions in a loop evaluation. Simply because (in most languages) must be re-run constantly and that takes up CPU.

    But also, can be dangerous, too, if you’re deleting from that array. Of course, if I’m looping to do that, I always do step=”-1″ and work backwards.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *