Thursday, July 30, 2026

ColdFusion Loop Structures - The 5 Minute Mission

You don't have hours to spend ramping up on ColdFusion.
You need to get in. Get out and deliver. Let's do that.

Copy and paste this into a cffiddle.org session.
Yep. Remember to wrap it with <cfscript> tags. Feel free to thank me in the comments.

// ---------------------------------------------------------------------------------------
// Tutorials with a SMART Mission
// What does that look like?

// SMART: Specific, Measurable, Achievable, Revelant and Time-bound
// S: Loop through Structures
// M: Read and digest within 5 minutes
// A: Anyone new/familiar with programing can use them immediately
// R: Use a data in a common ways anyone will mentally and emotionally connnect with
// T: Read within 3 min, Research more within seconds and Experiment with immediately.
// ---------------------------------------------------------------------------------------

// A struct is a key/value container. No index. No promises about order.
pantryStruct = {
    "Apples": 12,
    "Bananas": 6,
    "Carrots": 20,
    "Milk": 2,
    "Bread": 4
};

// A struct of structs (nested). Category -> its own pantry struct.
groceryStruct = {
    "Fruits": {"Apples": 12, "Bananas": 6, "Grapes": 30},
    "Veggies": {"Carrots": 20, "Broccoli": 5}
};

missionIntroduction = "
    <p><b>ColdFusion Loops - Structures</b><br>
    Remember Structure Functions? We are talking about looping today.  Welcome to another workout.</p>

    <p><b>MISSION:</b> Operation Groceries. Count what Mama's got in the pantry.</p>

    <p><b>VITAL DATA</b><br>
    <b>pantryStruct</b> (#structCount(pantryStruct)# items): #serializeJSON(pantryStruct)#<br><br>
    <b>groceryStruct</b> (#structCount(groceryStruct)# categories): #serializeJSON(groceryStruct)#</p>

    <p><b>NEED TO KNOW</b><br>
    <b>No Guaranteed Order:</b> Arrays and lists loop in order, 1 to the end. Structs don't play that game.<br>
    A struct is a bag of keys, not a line of Humvees. Loop it twice, you might get two different orders.<br>
    Need a guaranteed order? Sort the keys yourself, or build the struct with <b>structNew('ordered')</b>.</p>
";
writeOutput(missionIntroduction);

writeOutput("<b>EXAMPLES</b><br>");

// 1. Loop through struct with for-in (order not guaranteed)
writeOutput("<p>1. Loop through pantryStruct (key/value) - For-In<br>");
writeOutput("<strong>Pantry:</strong><br>");
for (key in pantryStruct) {
    writeOutput(key & ": " & pantryStruct[key] & "<br>");
}
writeOutput("</p>"); // outputs each key/value pair, order may vary run to run

// 2a. Loop through struct with structEach (closure-based)
writeOutput("<p>2a. Loop through pantryStruct - structEach (closure)<br>");
writeOutput("<strong>Pantry:</strong><br>");
structEach(pantryStruct, function(key, value) {
    writeOutput(key & ": " & value & "<br>");
});
writeOutput("</p>"); // same job as for-in, just handed off to a function

// 2b. Loop through struct in sorted key order
writeOutput("<p>2b. Loop through pantryStruct in A-Z order - structKeyArray + arraySort<br>");
writeOutput("<strong>Pantry (sorted):</strong><br>");
sortedKeys = structKeyArray(pantryStruct); // pull the keys out into an array
arraySort(sortedKeys, "textnocase"); // now THAT array can be ordered
for (key in sortedKeys) {
    writeOutput(key & ": " & pantryStruct[key] & "<br>");
}
writeOutput("</p>"); // outputs Apples, Bananas, Bread, Carrots, Milk - guaranteed, because you forced it

// 3. Loop through a nested struct (loop within a loop)
writeOutput("<p>3. Loop through groceryStruct (nested) - For-In inside For-In<br>");
writeOutput("<strong>Groceries by Category:</strong><br>");
for (category in groceryStruct) {
    writeOutput("<u>" & category & "</u><br>");
    categoryStruct = groceryStruct[category];
    for (item in categoryStruct) {
        writeOutput("  " & item & ": " & categoryStruct[item] & "<br>");
    }
}
writeOutput("</p>"); // each category prints its own items underneath it

// Include any additional information that helps summarizes the above examples
additionalPoints = "<br><hr>
<p>
 <b>Additional Points:</b>
 <ul>
  <li><b>Empty Structs:</b> Same deal as empty lists and arrays. Nothing in it, the loop body never runs. It won't do jack-squat.</li>
  <li><b>for-in vs. structEach:</b> They do the same job. for-in reads cleaner in most scripts. structEach hands the work to a function with some decent features.</li>
  <li><b>Order Matters? Sort It:</b> Don't assume insertion order. Pull structKeyArray, sort it, then loop the sorted array.  With that, you won't be doing KP, the other guy will.</li>
 </ul>
 </p>";
writeOutput(additionalPoints);

Let me know if I've missed your favorite loop. Or better yet point me to another site where everyone can learn about a riveting loop for structures from ColdFusion.

MORE INPUT
After you've climbed the rope and rung the bell of this code, go have a look at ColdFusion documentation at Adobe and Foundeo's CFDocs.

No comments: