Sunday, July 12, 2026

ColdFusion List Functions - 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: List 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 lists are strings separated by a delimiter (default is a comma)
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";

missionIntroduction = "
    <p><b>ColdFusion List 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 lists.</p>

    <p><b>VITAL DATA</b><br>
    <b>Fruits</b> (#listLen(fruitList)#): #fruitList#<br>
    <b>Veggies</b> (#listLen(veggieList)#): #veggieList#<br>
    <b>Meat</b> (#listLen(meatList)#): #meatList#</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. ListLen - Count the items in the fruit list
totalFruits = ListLen(fruitList);
writeOutput("1. <b>ListLen(fruitList)</b> - Total fruits: #totalFruits#<br>");
// Output: 10

// 2. ListGetAt - Fetch a specific item by its 1-based index
thirdFruit = ListGetAt(fruitList, 3);
writeOutput("2. <b>ListGetAt(fruitList, 3)</b> - 3rd fruit: #thirdFruit#<br>");
// Output: Blueberries

// 3. ListFindNoCase - Search for an item ignoring capital letters
veggieIndexNoCase = ListFindNoCase(veggieList, "potATO");
writeOutput("3. <b>ListFindNoCase(veggieList, 'potATO')</b> - 'potATO' found at position: #veggieIndexNoCase# <em>(not case-sensitive)</em><br>");
// Output: 7

// 4. ListContains - Search for an item that *contains* a partial string
// 'flower' is part of 'Cauliflower'
partialSearchIndex = ListContains(veggieList, "flower");
writeOutput("4. <b>ListContains(veggieList, 'flower')</b> - Element containing 'flower' (Cauliflower) at position: #partialSearchIndex#<br>");
// Output: 4

// 5. ListFind - Search for an item matching exact case sensitivity
veggieIndexCase = ListFind(veggieList, "potato"); 
writeOutput("5. <b>ListFind(veggieList, 'potato')</b> - Exact match 'potato' position (0 means not found): #veggieIndexCase# <em>(case-sensitive)</em><br>");
// Output: 0 (it is capitalized as 'Potato')

// 6. ListAppend - Add meat list to the vegetable list to make a dinner list
dinnerList = ListAppend(veggieList, meatList);
writeOutput("6. <b>ListAppend(veggieList, meatList)</b> - Combined Veggie + Meat list: #dinnerList#<br>");
// both veggieList and meatList all together

// 7. ListPrepend - Add lamb list to the meat list
meatList = ListPrepend(meatList, "Lamb");
writeOutput("7. <b>ListPrepend(meatList, 'Lamb') </b> - #meatList#<br>");
// Output: Lamb is in the first index

// 8. ListFirst - Grab the very first item from the combined dinner list
firstDinnerItem = ListFirst(dinnerList);
writeOutput("8. <b>ListFirst(dinnerList)</b> - First item to cook: #firstDinnerItem#<br>");
// Output: Spinach

// 9. ListLast - Grab the very last item from the combined dinner list
lastDinnerItem = ListLast(dinnerList);
writeOutput("9. <b>ListLast(dinnerList)</b> - Last item to cook: #lastDinnerItem#<br>");
// Output: Pork

// 10. ListDeleteAt - Remove the 2nd element (Kale) from the vegetable list
shortVeggieList = ListDeleteAt(veggieList, 2);
writeOutput("10. <b>ListDeleteAt(veggieList, 2)</b> - Veggies after removing index 2: #shortVeggieList#<br>");

// 11. ListChangeDelims - Swap the commas for pipes (|) to change data formatting
pipeDelimitedMeat = ListChangeDelims(meatList, "|");
writeOutput("11. <b>ListChangeDelims(meatList, '|')</b> - Meat list with new delimiters: #pipeDelimitedMeat#<br>");

// Include any additional information that helps summarizes the above examples
additionalPoints = "<br><hr>
<p>
 <strong>Additional Points:</strong>
 <ul>
  <li><b>Strings Under the Hood:</b> ListAppend does not alter the original variable dynamically; you must assign it back to a variable (e.g., dinnerList = ListAppend(...)).</li>
  <li><b>Find vs. Contains:</b> ListFind expects an exact match of the whole list item, but ListContains matches partial substrings inside an item.  They search differently.</li>
 </ul>
 </p>";
writeOutput(additionalPoints);
Let me know if I've missed your favorite list function. Or better yet point me to another article where everyone can learn about about a nifty List function from ColdFusion.

No comments: