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: Structure 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 structures are variables that have a "key" and "data" format
fruitStruct = {
"A": "Apples",
"B": "Bananas",
"C": "Cantaloupe",
"G": "Grapes",
"K": "Kiwi",
"O": "Oranges",
"P": "Pomegranate",
"S": "Strawberries",
"W": "Watermelon"
};
meatStruct = {
"B": "Beef",
"C": "Chicken",
"F": "Fish",
"L": "Lamb",
"P": "Pork",
"T": "Turkey"
};
missionIntroduction = "
<p><b>ColdFusion Structure Functions</b><br>
As a CF developer, you use these often. Pick these up. You gonna need all 6 of 'em.</p>
<p><b>MISSION:</b> Operation Groceries. Go help your Mama with these two structures.</p>
<p><b>VITAL DATA</b><br>
<b>Fruits</b> (#structCount(fruitStruct)#): #serializeJSON(fruitStruct)#<br>
<b>Meat</b> (#structCount(meatStruct)#): #serializeJSON(meatStruct)#</p>
<p><b>NEED TO KNOW</b><br>
<b>Keys:</b> The keys of a structure are not case-sensitive by default.<br>
In Adobe ColdFusion, you can define them as case-sensitive if you want to since CF 2021.<br>
Lucee does not have this feature. Sorry Charlie! Now you know.</p>
";
writeOutput(missionIntroduction);
writeOutput("<b>EXAMPLES</b><br>");
// 1. structCount - Count items in Struct
totalFruits = structCount(fruitStruct);
writeOutput("1. <b>structCount(fruitStruct)</b><br>Total Fruit Count: #totalFruits#<br>");
// 2. structInsert - add to struct (a struct is not linear, it just a container)
structInsert(fruitStruct, "M", "Mangoes"); // Add Mangoes to the structure
writeOutput("2. <b>structInsert(fruitStruct, 'M', 'Mangoes')</b><br>" & serializeJSON(fruitStruct) & "<br>");
// What if the Key already exists? structInsert will fail. See structKeyExists function
try {
structInsert(fruitStruct, "M", "Monkfruit", false);
}
catch(any e) {
writeOutput("<br><em>ATTEMPT & FAIL</em>");
writeOutput("<br>TASK: Try to insert a key that already exists");
writeOutput(" - <b>structInsert(fruitStruct, 'M', 'Monkfruit', false)</b>");
writeOutput("<br>ERROR INFO<br>Message: #e.message#<br>Detail: #e.detail#<br><br>");
}
// 3. structKeyExists - Check existence and if it does exist, show key's value
favoriteFruit = "Not yet found";
keyToFind = "K"; // Feel free to change this to "Q" to prove when Key does not exist
writeOutput("3. <b>structKeyExists(fruitStruct, #keyToFind#)</b>");
if (structKeyExists(fruitStruct, keyToFind)) {
favoriteFruit = structFind(fruitStruct, keyToFind); // return value of key
writeOutput(" & <b>structFind(fruitStruct, #keyToFind#)</b><br>")
writeOutput("Key '#keyToFind#' exists! The value is: " & favoriteFruit & "<br>");
}
else{
writeOutput(" & <b>structFind(fruitStruct, #keyToFind#)</b><br>");
writeOutput("Key '#keyToFInd#' does NOT exist! The value is: " & favoriteFruit & "<br>");
}
// 4. structDelete - Remove key from Struct
deleteKeyW = structDelete(fruitStruct, "W"); // Removes Watermelon from the structure
writeOutput("4a. <b>structDelete(fruitStruct, 'W')</b> - #deleteKeyW# (did exist in Structure)<br>" & serializeJSON(fruitStruct) & "<br>");
// BONUS DISCOVERY: If you tried to delete a Key that did not exist,
// you need the 3rd parameter "indicateNotExisting" to get an accurate boolean of false. Strange but true. Keep your boots laced.
deleteKeyQ = structDelete(fruitStruct, "Q", true);
writeOutput("4b.<b>structDelete(fruitStruct, 'Q')</b> - Delete Key Q: #deleteKeyQ# (does not exist in Structure)<br>");
// 5. structAppend - Append one structure to another one
// Create a separate copy to show appending without destroying our original fruitStruct
appendFruityMeatStruct = duplicate(fruitStruct);
// Merges meatStruct into appendDemoStruct.
structAppend(appendFruityMeatStruct, meatStruct, false); // False = will NOT overwrite matching keys (B, C, P)
writeOutput("5. <b>structAppend(fruitStruct, meatStruct, false)</b><br>");
writeOutput("<em>(Nope! Pork, Beef and Chicken didn't make the cut. A key already existed. Remember: Override = false)</em><br>");
writeOutput(serializeJSON(appendFruityMeatStruct) & "<br>");
// Include any additional information that helps summarizes the above examples
additionalPoints = "<br><hr>
<p>
<b>Additional Points:</b>
<ul>
<li><b>Keys are Not Case-sensitive:</b> This is by default. It could be you assign new data to an existing key and you don't see it updated. If you wanted it to -- use the Overwrite flag Luke. I am your Father.</li>
<li><b>Nested Structures</b> Yeah, it's possible. We didn't talk about that here, but yeah. Nesting can quickly become more challenging. Don't fear it. Embrace it.</li>
<li><b>Structures Under the Hood:</b> Structures may have a count of total items, but you cannot use a formal indexed for loop. Loops for Structures on the next tutorial.</li>
</ul>
</p>";
writeOutput(additionalPoints);
Let me know if I've missed your favorite structure function. Or better yet drop a comment so everyone can learn about a brave/bold structure 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.