Whatever message this page gives is out now! Go check it out!
ListToArray(list [, delimiters[, includeEmptyFields[, multiCharacterDelimiter]]])Parameter | Description |
list | A list or a variable that contains one.You define a list variable with a cfset statement. |
delimiters | A string or a variable that contains one. ColdFusion treats each character in the string as a delimiter. The default value is comma. |
includeEmptyFields | A Boolean value specifying whether to create empty array entries if there are two delimiters in a row.
|
multiCharacterDelimiter | A Boolean value specifying whether the delimiters parameter specifies a multi-character delimiter. The default is false. If this parameter is true, the delimiters parameter must specify a single delimiter consisting of multiple characters. This parameter enables the ListToArray function to convert a list such as the following to an array of color names: red:|orange:|yellow:|green:|blue:|indigo:|violet. |
<cfset list = "red,orange,&yellow,green,&blue,violet">
<cfset arr = listToArray (list, ",&",false,true)><cfscript>
myList="ten;twenty;thirty;fort;fifty;sixty;;seventy;;;eighty"; // list contains empty elements
// convert myList to myArray
myArray=listToArray(myList,";",false,true); // Using includeEmptyFields=false
// Display array elements
WriteDump(myArray);
</cfscript><cfscript>
myList="ten;twenty;thirty;fort;fifty;sixty;;seventy;;;eighty"; // list contains empty elements
// convert myList to myArray
myArray=listToArray(myList,";",true,true); // Using includeEmptyFields=true
// Display array elements
WriteDump(myArray);
</cfscript><cfscript>
writeOutput("<h2>📦 USE CASE 1: CSV Product Data Processing</h2>");
writeOutput("<h3>Business Context:</h3>");
writeOutput("<p>An e-commerce company receives daily product updates from suppliers in CSV format. Each product line contains categories, tags, and features that need to be processed individually for database storage and search indexing.</p>");
// Sample CSV product data from supplier
productCsvData = "Electronics,Laptops,Gaming Notebooks,High-Performance,RGB Lighting,SSD Storage,16GB RAM";
writeOutput("<h4>📥 Raw CSV Data:</h4>");
writeOutput("<p><code>" & productCsvData & "</code></p>");
// Convert CSV to array for processing
productArray = ListToArray(productCsvData, ",", false);
writeOutput("<h4>🔄 Processing Results:</h4>");
writeOutput("<table border='1' cellpadding='8'>");
writeOutput("<tr><th>Index</th><th>Product Attribute</th><th>Processing Action</th></tr>");
for (i = 1; i <= arrayLen(productArray); i++) {
attribute = trim(productArray[i]);
// Business logic for each attribute type
if (i == 1) {
action = "Primary Category Assignment";
category = attribute;
} else if (i == 2) {
action = "Subcategory Classification";
subcategory = attribute;
} else {
action = "Feature Tag for Search Index";
}
writeOutput("<tr>");
writeOutput("<td>" & i & "</td>");
writeOutput("<td>" & attribute & "</td>");
writeOutput("<td>" & action & "</td>");
writeOutput("</tr>");
}
writeOutput("</table>");
// Database insertion simulation
writeOutput("<h4>💾 Database Operations:</h4>");
writeOutput("<p><strong>Primary Category:</strong> " & (category ?: "N/A") & "</p>");
writeOutput("<p><strong>Subcategory:</strong> " & (subcategory ?: "N/A") & "</p>");
writeOutput("<p><strong>Feature Tags:</strong> " & arrayLen(productArray) - 2 & " tags indexed for search</p>");
writeOutput("<h4>💼 Business Value:</h4>");
writeOutput("<ul>");
writeOutput("<li>Automated processing of 1000+ products daily</li>");
writeOutput("<li>Consistent data structure for search optimization</li>");
writeOutput("<li>Reduced manual data entry errors by 95%</li>");
writeOutput("<li>Faster time-to-market for new products</li>");
writeOutput("</ul>");
writeOutput("<hr>");
</cfscript><cfscript>
writeOutput("<h2>👥 USE CASE 2: User Permissions & Role Management</h2>");
writeOutput("<h3>Business Context:</h3>");
writeOutput("<p>A corporate CRM system stores user permissions as comma-separated lists in the database. When users log in, these permissions need to be converted to arrays for role-based access control throughout the application.</p>");
// Sample user permission data (includes empty elements for inactive roles)
userPermissions = "read_customers,write_customers,,read_reports,admin_users,,delete_records";
writeOutput("<h4>📥 Raw Permission String:</h4>");
writeOutput("<p><code>" & userPermissions & "</code></p>");
writeOutput("<p><em>Note: Empty elements represent deactivated permissions</em></p>");
// Process permissions excluding empty fields (inactive permissions)
activePermissions = ListToArray(userPermissions, ",", false);
writeOutput("<h4>✅ Active Permissions Processing:</h4>");
writeOutput("<p><strong>includeEmptyFields = false</strong></p>");
permissionCategories = {
"read": [],
"write": [],
"admin": [],
"delete": []
};
for (permission in activePermissions) {
cleanPermission = trim(permission);
// Categorize permissions by type
if (findNoCase("read_", cleanPermission)) {
arrayAppend(permissionCategories.read, cleanPermission);
} else if (findNoCase("write_", cleanPermission)) {
arrayAppend(permissionCategories.write, cleanPermission);
} else if (findNoCase("admin_", cleanPermission)) {
arrayAppend(permissionCategories.admin, cleanPermission);
} else if (findNoCase("delete_", cleanPermission)) {
arrayAppend(permissionCategories.delete, cleanPermission);
}
}
writeOutput("<table border='1' cellpadding='8'>");
writeOutput("<tr><th>Permission Level</th><th>Granted Permissions</th><th>Count</th></tr>");
for (level in permissionCategories) {
permissions = permissionCategories[level];
writeOutput("<tr>");
writeOutput("<td><strong>" & uCase(level) & "</strong></td>");
writeOutput("<td>" & (arrayLen(permissions) > 0 ? arrayToList(permissions, ", ") : "None") & "</td>");
writeOutput("<td>" & arrayLen(permissions) & "</td>");
writeOutput("</tr>");
}
writeOutput("</table>");
// Now process with empty fields to see all assigned slots
allPermissionSlots = ListToArray(userPermissions, ",", true);
writeOutput("<h4>📊 Permission Audit (including empty slots):</h4>");
writeOutput("<p><strong>includeEmptyFields = true</strong> - Shows all permission slots including deactivated ones</p>");
writeOutput("<table border='1' cellpadding='8'>");
// writeOutput("<tr><th>Slot #</th><th>Permission</th><th>Status</th></tr>");
for (i = 1; i <= arrayLen(allPermissionSlots); i++) {
permission = trim(allPermissionSlots[i]);
status = len(permission) > 0 ? "Active" : "Deactivated";
statusColor = len(permission) > 0 ? "green" : "red";
writeOutput("<tr>");
writeOutput("<td>" & i & "</td>");
writeOutput("<td>" & (len(permission) > 0 ? permission : "<em>Empty Slot</em>") & "</td>");
writeOutput("<td style='color: " & statusColor & ";'><strong>" & status & "</strong></td>");
writeOutput("</tr>");
}
writeOutput("</table>");
writeOutput("<h4>💼 Business Value:</h4>");
writeOutput("<ul>");
writeOutput("<li>Granular access control for 500+ enterprise users</li>");
writeOutput("<li>Easy permission auditing and compliance reporting</li>");
writeOutput("<li>Flexible role management without database schema changes</li>");
writeOutput("<li>Reduced security vulnerabilities through systematic access control</li>");
writeOutput("</ul>");
writeOutput("<hr>");
</cfscript><cfscript>
writeOutput("<h2>🌍 USE CASE 3: Multi-Language Content Processing</h2>");
writeOutput("<h3>Business Context:</h3>");
writeOutput("<p>A global e-learning platform stores course content in multiple languages separated by multi-character delimiters. This allows content managers to maintain translations in a single field while enabling easy separation for display.</p>");
// Course content with multi-language translations separated by special delimiter
courseContent = "Introduction to Programming||Introducción a la Programación||Einführung in die Programmierung||プログラミング入門||프로그래밍 소개";
writeOutput("<h4>📥 Multi-Language Content String:</h4>");
writeOutput("<p><code>" & courseContent & "</code></p>");
writeOutput("<p><em>Languages: English || Spanish || German || Japanese || Korean</em></p>");
// Process with multi-character delimiter
languageArray = ListToArray(courseContent, "||", false, true);
writeOutput("<h4>🔄 Language Processing Results:</h4>");
// Language codes mapping
languageCodes = ["EN", "ES", "DE", "JA", "KO"];
languageNames = ["English", "Spanish", "German", "Japanese", "Korean"];
writeOutput("<table border='1' cellpadding='8'>");
writeOutput("<tr><th>Language Code</th><th>Language</th><th>Translated Content</th><th>Character Count</th></tr>");
for (i = 1; i <= arrayLen(languageArray); i++) {
if (i <= arrayLen(languageCodes)) {
content = trim(languageArray[i]);
writeOutput("<tr>");
writeOutput("<td><strong>" & languageCodes[i] & "</strong></td>");
writeOutput("<td>" & languageNames[i] & "</td>");
writeOutput("<td>" & content & "</td>");
writeOutput("<td>" & len(content) & "</td>");
writeOutput("</tr>");
}
}
writeOutput("</table>");
// Content localization function
function getLocalizedContent(contentArray, userLanguage) {
languageMap = {
"en": 1,
"es": 2,
"de": 3,
"ja": 4,
"ko": 5
};
languageIndex = languageMap[lCase(userLanguage)] ?: 1;
if (languageIndex <= arrayLen(contentArray)) {
return trim(contentArray[languageIndex]);
}
return trim(contentArray[1]); // Fallback to English
}
// Demonstrate localization for different users
writeOutput("<h4>🎯 User Localization Examples:</h4>");
testUsers = [
{lang: "en", name: "John (USA)"},
{lang: "es", name: "María (Mexico)"},
{lang: "de", name: "Hans (Germany)"},
{lang: "ja", name: "田中 (Japan)"},
{lang: "ko", name: "김철수 (Korea)"}
];
writeOutput("<table border='1' cellpadding='8'>");
writeOutput("<tr><th>User</th><th>Preferred Language</th><th>Localized Content</th></tr>");
for (user in testUsers) {
localizedContent = getLocalizedContent(languageArray, user.lang);
writeOutput("<tr>");
writeOutput("<td>" & user.name & "</td>");
writeOutput("<td>" & uCase(user.lang) & "</td>");
writeOutput("<td>" & localizedContent & "</td>");
writeOutput("</tr>");
}
writeOutput("</table>");
</cfscript><cfscript>
writeOutput("<h2>📊 USE CASE 4: Survey Response Analysis</h2>");
writeOutput("<h3>Business Context:</h3>");
writeOutput("<p>A market research company collects customer feedback through online surveys. Multiple-choice questions with checkboxes are stored as comma-separated lists. Analytics need to process these responses to generate insights and reports.</p>");
// Sample survey responses for "Which features are most important to you?"
surveyResponses = [
"Price,Quality,Customer Service,Warranty",
"Quality,Design,Brand Reputation,Warranty,",
"Price,,Customer Service,Fast Delivery,Warranty",
"Quality,Design,Brand Reputation",
"Price,Quality,,Fast Delivery,"
];
writeOutput("<h4>📥 Raw Survey Data:</h4>");
writeOutput("<p><em>Question: Which features are most important to you? (Check all that apply)</em></p>");
featureCount = {};
totalResponses = arrayLen(surveyResponses);
writeOutput("<table border='1' cellpadding='8'>");
//writeOutput("<tr><th>Response #</th><th>Raw Data</th><th>Selected Features</th><th>Selection Count</th></tr>");
for (i = 1; i <= arrayLen(surveyResponses); i++) {
response = surveyResponses[i];
// Process response excluding empty fields (unchecked boxes)
selectedFeatures = ListToArray(response, ",", false);
writeOutput("<tr>");
writeOutput("<td>" & i & "</td>");
writeOutput("<td><code>" & response & "</code></td>");
writeOutput("<td>" & arrayToList(selectedFeatures, ", ") & "</td>");
writeOutput("<td>" & arrayLen(selectedFeatures) & "</td>");
writeOutput("</tr>");
// Count feature frequencies
for (feature in selectedFeatures) {
cleanFeature = trim(feature);
if (len(cleanFeature) > 0) {
if (!structKeyExists(featureCount, cleanFeature)) {
featureCount[cleanFeature] = 0;
}
featureCount[cleanFeature]++;
}
}
}
writeOutput("</table>");
// Generate analytics report
writeOutput("<h4>📈 Feature Popularity Analysis:</h4>");
// Sort features by popularity
featureStats = [];
for (feature in featureCount) {
count = featureCount[feature];
percentage = round((count / totalResponses) * 100);
arrayAppend(featureStats, {
feature: feature,
count: count,
percentage: percentage
});
}
// Sort by count (descending)
arraySort(featureStats, function(a, b) {
return b.count - a.count;
});
writeOutput("<table border='1' cellpadding='8'>");
writeOutput("<tr><th>Rank</th><th>Feature</th><th>Responses</th><th>Percentage</th><th>Priority Level</th></tr>");
for (i = 1; i <= arrayLen(featureStats); i++) {
stat = featureStats[i];
// Determine priority level
if (stat.percentage >= 80) {
priority = "Critical";
priorityColor = "red";
} else if (stat.percentage >= 60) {
priority = "High";
priorityColor = "orange";
} else if (stat.percentage >= 40) {
priority = "Medium";
priorityColor = "blue";
} else {
priority = "Low";
priorityColor = "gray";
}
writeOutput("<tr>");
writeOutput("<td>" & i & "</td>");
writeOutput("<td><strong>" & stat.feature & "</strong></td>");
writeOutput("<td>" & stat.count & " / " & totalResponses & "</td>");
writeOutput("<td>" & stat.percentage & "%</td>");
writeOutput("<td style='color: " & priorityColor & ";'><strong>" & priority & "</strong></td>");
writeOutput("</tr>");
}
writeOutput("</table>");
// Business recommendations
writeOutput("<h4>🎯 Business Recommendations:</h4>");
writeOutput("<ul>");
for (i = 1; i <= min(3, arrayLen(featureStats)); i++) {
stat = featureStats[i];
writeOutput("<li><strong>" & stat.feature & "</strong>: Top priority - mentioned by " & stat.percentage & "% of respondents</li>");
}
</cfscript>