ColdFusion ListToArray – includeEmptyField
Most ColdFusion developers have probably realized at some point that ListToArray ignores empty values between delimiters altogether. What I mean is that "a,,,b,c,d" would return an array with only 4 items in it, rather than 6 items with two of them being empty. This has been frustrating to say the least. ColdFusion 8 to the rescue, ListToArray now has a third arguments "includeEmptyField" that defaults to false, but if true will not ignore empty values. See the following code for an example:
ColdFusion:
-
<cfset letters="a,,,b,c,d" />
-
<cfdump var="#listToArray(letters,',',true)#" label="listToArray(letters,',',true)" />
-
<cfdump var="#listToArray(letters,',',false)#" label="listToArray(letters,',',false)" />
The above code results in the following output. Nothing unexpected here, but a welcome addition nonetheless.
I really have to read the docs better, I missed that extra attribute that was passed in.
very cool
I agree it is cool. However, I also maintain this is more of a fix than an enhancement since to me it seemed crazy that there was no way to account for blank values before outside of replacing empty values with an arbitrary value.
There is an easy way around everything just replace the delimiter delimiter with demlimiter space delimiter than trim the values when you are using them
Good point Dirk, although it is nice to no longer have to use a workaround.