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 over strings, lists and arrays
// 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.
// ---------------------------------------------------------------------------------------
// String is a variable is made up of characters.
missionString = "GROCERIES";
// Lists are strings with a delimiter
fruitList = "Apples,Bananas,Blueberries,Strawberries,Grapes,Oranges,Cantaloupe,Watermelon,Kiwi,Pomegranate";
veggieList = "Spinach,Kale,Broccoli,Cauliflower,Radish,Carrot,Potato,Pepper,Corn,Onion";
meatList = "Chicken,Turkey,Fish,Beef,Pork";
// ColdFusion arrays are variables like a giant wall of shelves. You can keep your stuff there.
fruitArray = ["Apples", "Bananas", "Blueberries", "Strawberries", "Grapes", "Oranges", "Cantaloupe", "Watermelon", "Kiwi", "Pomegranate"];
veggieArray = ["Spinach", "Kale", "Broccoli", "Cauliflower", "Radish", "Carrot", "Potato", "Pepper", "Corn", "Onion"];
meatArray = ["Chicken", "Turkey", "Fish", "Beef", "Pork"];
missionIntroduction = "
<p><b>ColdFusion Loops</b><br>
As a CF developer, your variables are your variables. Love them.<br>
Say it. They are mine. There are many like them. But these are mine.</p>
<p><b>MISSION:</b> Operation Groceries. Go help your Mama with these variables.</p>
<p><b>VITAL DATA</b><br>
<b>missionString</b> : #missionString# (#len(missionString)# characters in length)<br><br>
<b>Fruits</b> (#arrayLen(fruitArray)#): #ArrayToList(fruitArray)#<br>
<b>Veggies</b> (#arrayLen(veggieArray)#): #ArrayToList(veggieArray)#<br>
<b>Meat</b> (#arrayLen(meatArray)#): #ArrayToList(meatArray)#</p>
<p><b>NEED TO KNOW</b><br><b>1-Based Indexing:</b> ColdFusion lists start at index 1.<br>
Yes, they start at 1. What the heck? It's ColdFusion's thing.<br>
Call it a quirk. Get used to it. Love the number 1 and like it.<br>
Other languages have their indexes start at 0 like JavaScript, C++, Java or Python.</p>
";
writeOutput(missionIntroduction);
writeOutput("<b>EXAMPLES</b><br>");
// 1. Loop through strings
writeOutput("1. Loop through strings (one character at a time):<br>");
for (i = 1; i <= len(missionString); i++) {
char = mid(missionString, i, 1); // Get single character from string
writeOutput(char & " ");
}
writeOutput("<br>"); // outputs: G R O C E R I E S
// 2a: Loop through fruitList with for-in
writeOutput("<p>2a. Loop through Fruit lists (one item at a time) - For-In<br>");
writeOutput("<strong>Fruit List:</strong><br>");
for (item in fruitList) {
writeOutput(item & "<br>");
}
writeOutput("</p>"); // outputs each fruit on a new line
// 2b: Loop through meatList with index-based for
writeOutput("<p>2b. Loop through Meatlists (one item at a time) - Index-Based for<br>");
writeOutput("<strong>Meat List:</strong><br>");
for (i = 1; i <= listLen(meatList); i++) {
item = listGetAt(meatList, i);
writeOutput(i & ": " & item & "<br>");
}
writeOutput("</p>"); // outputs each meat with its index on a new line
// 3a: Loop through veggieArray with for-in
writeOutput("<p>3a. Loop through Veggie arrays (one item at a time) - For-In<br>");
writeOutput("<strong>Veggie Array:</strong><br>");
for (item in veggieArray) {
writeOutput(item & "<br>");
}
writeOutput("</p>"); // outputs each veggie on a new line
// 3b: Loop through fruitArray with index-based for
writeOutput("<p>3b. Loop through fruitArray (one item at a time) - Index-based for:<br>");
writeOutput("<strong>Fruit Array:</strong><br>");
for (i = 1; i <= arrayLen(fruitArray); i++) {
writeOutput(i & ": " & fruitArray[i] & "<br>");
}
writeOutput("</p>"); // outputs each fruit with its index on a new line
// Include any additional information that helps summarizes the above examples
additionalPoints = "<br><hr>
<p>
<strong>Additional Points:</strong>
<ul>
<li><b>Loops Under the Hood:</b> Loops can start any where but if you loop through strings, lists and arrays they start at 1. Yep! Index-based at 1.</li>
<li><b>Empty Lists or Arrays:</b> It happens. If a list or array has no items, it won't enter the loop. It won't do jack-squat. There's nothing to loop over. Keep that in mind.</li>
<li><b>Looping Over Structs:</b> We haven't talked about those yet. They are the grub, you've been waiting for bub. You need those. But that's a whole other tutorial.</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 loop from ColdFusion.
MORE INPUT
After your done with your morning run at this code, go have a look at ColdFusion documentation at
Adobe
and Foundeo's CFDocs.