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: Array Functions
// 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.
// ---------------------------------------------------------------------------------------
// 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 Array Functions</b>
As a CF developer, you use these on a daily basis. Pick these up. You gonna need all 11 of 'em.</p>
<p><b>MISSION:</b> Operation Groceries. Go help your Mama with these three arrays.</p>
<p><b>VITAL DATA</b><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. arrayLen - Count items in fruit Array
totalFruits = arrayLen(fruitArray);
writeOutput("1. <b>arrayLen(fruitArray)</b> - Total fruits: #totalFruits#<br>");
// Output: 10
// 2. arrayAppend - add to end
ArrayAppend(fruitArray, "Mango");
WriteOutput("2. arrayAppend(fruitArray, 'Mango') - " & fruitArray[ArrayLen(fruitArray)] & "<br>");
// ["Apples", "Bananas", "Blueberries", "Strawberries", "Grapes", "Oranges", "Cantaloupe", "Watermelon", "Kiwi", "Pomegranate", "Mango"]
// 2. arrayPrepend: add to start
ArrayPrepend(veggieArray, "Garlic");
WriteOutput("3. arrayPrepend(veggieArray, 'Garlic') - " & veggieArray[1] & "<br>");
// ["Garlic", "Spinach", "Kale", "Broccoli", "Cauliflower", "Radish", "Carrot", "Potato", "Pepper", "Corn", "Onion"]
// 4. arrayInsertAt: Insert at specific index (shifts others down)
ArrayInsertAt(meatArray, 2, "Lamb"); // Insert "Lamb" at index 2 in meatArray
WriteOutput("4. arrayInsertAt(meatArray, 2, 'Lamb') - " & meatArray[2] & "<br>");
// ["Chicken", "Lamb", "Turkey", "Fish", "Beef", "Pork"]
// 5. arrayDeleteAt: Remove item at index 5 (Grapes)
ArrayDeleteAt(fruitArray, 5);
WriteOutput("5. ArrayDeleteAt(fruitArray, 5) - " & fruitArray[5] & "<br>");
// ["Apples", "Bananas", "Blueberries", "Strawberries", "Oranges", "Cantaloupe", "Watermelon", "Kiwi", "Pomegranate", "Mango"]
// 6. arrayFind: Case-sensitive (returns index or 0 if not found)
pos = ArrayFind(fruitArray, "kiwi");
WriteOutput("6. ArrayFind(fruitArray, 'kiwi') - " & pos & " (0 = not found)<br>");
// Expected: 0 (not found)
// 7. arrayFindNoCase: Case-insensitive search (returns index or 0 if not found)
posNoCase = ArrayFindNoCase(fruitArray, "kiwi");
WriteOutput("7. arrayFindNoCase(fruitArray, 'kiwi') - " & posNoCase & " (9 = found)<br>");
// Expected: 9 (or current index)
// 8. arrayContains: Returns Boolean if array contains value
hasBroccoli = ArrayContains(veggieArray, "Broccoli");
WriteOutput("8a. arrayContains(veggieArray, 'Broccoli') - " & hasBroccoli & "<br>");
// Expected: true
hasRice = ArrayContains(veggieArray, "Rice");
WriteOutput("8b. arrayContains(veggieArray, 'Rice') - " & hasRice & "<br>");
// Expected: false
// 9. arraySlice: Extract subset (Start Index, Length)
firstThreeVeggies = ArraySlice(veggieArray, 1, 3);
WriteOutput("9. arraySlice(veggieArray, 1, 3) - " & ArrayToList(firstThreeVeggies) & "<br>");
// Expected: Garlic, Spinach, Kale
// 10. ArrayToList: Convert to string
meatCSV = ArrayToList(meatArray);
WriteOutput("10. arrayToList(meatArray) - " & meatCSV & "<br>");
// Output: Chicken,Lamb,Turkey,Fish,Beef,Pork
// ArrayToList with custom delimiter
fruitPipe = ArrayToList(fruitArray, "|");
WriteOutput("11. arrayToList(fruitArray, '|') - " & fruitPipe & "<br>");
// Output: Apples|Bananas|Blueberries|Strawberries|Oranges|Cantaloupe|Watermelon|Kiwi|Pomegranate|Mango
// Include any additional information that helps summarizes the above examples
additionalPoints = "<br><hr>
<p>
<strong>Additional Points:</strong>
<ul>
<li><b>Arrays Under the Hood:</b> ArrayAppend alters the original variable; yeah, it's cool. Keep the change.</li>
<li><b>Find vs. Contains:</b> ArrayFind expects an exact match of the whole list item, but ArrayContains matches partial substrings inside an item. They search differently. Shove that in your cargo pant pocket.</li>
</ul>
</p>";
writeOutput(additionalPoints);
Let me know if I've missed your favorite array function. Or better yet point me to another site where everyone can learn about a hearty array function from ColdFusion.
MORE INPUT
After you've shot your full mag at this code, go have a look at ColdFusion documentation at Adobe and Foundeo's CFDocs.
No comments:
Post a Comment