Search code examples
coldfusioncoldfusion-10

Removing duplicates from a list


I have a list of strings and I need to remove the duplicates. I have tried a number of things, such as:

Unfortunately, none of them worked. I'm really not sure what is going on. So any help would be appreciated.

I am currently using a free Developer's version of ColdFusion 10 in case that affects things.

Sample List:

lacunar_DM, Homocysteine, HTN, Tobacco, undetermined ,lacunar_DM,Homocysteine,Tobacco

This was created by appending a static list with a dynamic one pulled from a database:

<cfsavecontent variable= "lacunar_list">
lacunar_DM,
Homocysteine,
HTN,
Tobacco,
undetermined
</cfsavecontent>
<cfset combination = ListAppend(lacunar_list, lacunar)>

<cfoutput>
List before removing dups: #combination#<br/>
List after removing dups: #listremoveduplicates(combination, ",", true)#<br/>
</cfoutput>

Here are the results:

List before removing dups:

lacunar_DM, Homocysteine, HTN, Tobacco, undetermined ,lacunar_DM,Homocysteine,Tobacco

List after removing dups:

lacunar_DM, Homocysteine, HTN, Tobacco, undetermined ,lacunar_DM,Homocysteine,Tobacco


Solution

  • The problem is that the list which you created with cfsavecontent has newline characters in it because each item in that list is on it's own line within cfsavecontent. Since the other list you are joining it with does not have the same whitespace, you are not going to get the correct results.

    Generally, it is best to remove unnecessary whitespace from lists in Coldfusion.

    Try this instead of using cfsavecontent:

    <cfset lacunar_list = "lacunar_DM,Homocysteine,HTN,Tobacco,undetermined" >
    <cfset combination = ListAppend(lacunar_list, lacunar)>