Thursday, July 19, 2007

ListToArray in ColdFusion 8

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 :-)

10 comments:

Sam Farmer said...

Cool. Nice addition. Glad you left something for CF9! ;)

Anonymous said...

There won't be a CF9!
Everyone will die soon! :)

gary gilbert said...

Great addition to CF8. That has been something that I've wanted in CF for ages!

Rupesh Kumar said...

Thanks Sam and Gary. Didn't we say that we listen to our users :-)

Giancarlo Gomez said...

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#" />

Rupesh Kumar said...

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.

halhelms said...

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

Rupesh Kumar said...

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>

Tom Mollerus said...

@Hal, aren't you using the syntax to create a structure? That may be why your loop isn't working.

Anonymous said...

Plus it would break anyways because his variable is set to #a not #a#