There has been lots and lots of discussion in CF blogs and forums about ListToArray not supporting empty elements. Seeing which I had blogged about a simple way which could do the same. Thanks to Charlie Griefer for pointing out the problem in it and then thanks to Ben Nadel and Andrew Clark for pointing me in the right direction.
Though it got pretty late, I was able to sneak-in this change in ColdFusion 8. ListToArray() now takes an additional optional argument “includeEmptyElements”, which if ‘true’ will include the empty elements of list into the array. Default is of course ‘false’. It also takes care of empty elements at the end of list and multiple delimiters. Here is how the function looks
ListToArray(list, delimiter, includeEmptyElements) returns Array
Lets take a look at couple of examples to see it working
<cfset list = "a,b,,c, ,d,,"> <cfset arr = ListToArray(list, ',', true)> <cfdump var="#arr#">
Here is how the output looks.
Here is another example.
<cfset list = "one,/$/,six"> <cfset arr = listToArray(list, ",$/",true)> <cfdump var="#arr#">
The output for which looks like this
Though we wanted to, there was just not enough time to make similar change in all the list functions for CF 8. Something for CF 9

#1 by Sam Farmer on July 19th, 2007
| Quote
Cool. Nice addition. Glad you left something for CF9!
#2 by Anonymous on July 19th, 2007
| Quote
There won’t be a CF9!
Everyone will die soon!
#3 by gary gilbert on July 23rd, 2007
| Quote
Great addition to CF8. That has been something that I’ve wanted in CF for ages!
#4 by Rupesh Kumar on July 24th, 2007
| Quote
Thanks Sam and Gary. Didn’t we say that we listen to our users
#5 by Giancarlo Gomez on August 5th, 2007
| Quote
Rupesh,
I tried this today on my box and got different results and can not figure our why. I used both the new listToArray in CF8 and the list2Array function from one of your previous post. The problem is that my delimiter is “,” (all three are the total delimiter). The list2array returns the correct amount of elements but CF8’s listToArray returns 24 elements when there should only be 8. Below is the code I tested.
<cfscript>
// Empty List Function
function list2Array(list)
{
var i = 0;
var retlist = ArrayNew(1);
var arr = list.split(’”,”‘,-1);
for(i = 1; i < = ArrayLen(arr); i++)
ArrayAppend(retList,arr[i]);
return retList;
}
theList=’”one”,”two”,”three”,”",”four”,”",”",”seven”‘;
theArray1=listToArray(theList,’”,”‘,true);
theArray2=list2Array(theList);
</cfscript>
<cfdump var=”#theArray1#” />
<cfdump var=”#theArray2#” />
#6 by Rupesh Kumar on August 6th, 2007
| Quote
Giancarlo,
In ColdFusion ListToArray function, when you specify multi-character delimiter, each character is treated as a separate delimiter and not the whole string. This behavior exists since ListToArray function was introduced. So when you had a delimiter ‘”,”‘, it treated ‘”‘ and ‘,’ as separate delimiters and hence 24 elements. If you want to have a multi-character delimiter, as in your example, you should use the UDF List2Array I had mentioned.
#7 by halhelms on August 8th, 2007
| Quote
Rupesh,
Unfortunately, if you use the new syntax for CF, the empty elements are not ignored when looping over the list.
<cfset a = {2,,4,5} />
<cfloop array=”#a” index=”anElement”>
#anElement#
<cfloop>
This will break
#8 by Rupesh Kumar on August 9th, 2007
| Quote
Hal,
I didn’t really understand what breaks.
New syntax for array does not even allow you to have empty elements in array.
So the following code will not run..
<cfset a = [2,4, ,5] />
<cfloop array=”#a#” index=”anElement”>
<cfoutput>#anElement#</cfoutput>
</cfloop>
#9 by Tom Mollerus on March 12th, 2008
| Quote
@Hal, aren’t you using the syntax to create a structure? That may be why your loop isn’t working.