Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Checking for duplicates in a listappend

Enthusiast ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

At certain points I add values into a variable using listappend, might be a single digit, might be a valuelist of numbers from a cfquery result.

The question I have is, does anybody know of a simple way to make sure that duplicates are not added to the list?

Apart from looping over the results of the cfquery and then checking each one to see if it is in the list and if not then adding it.

I'm wondering if CF8 has anything built in that I have overlooked?

Thanks

Mark

TOPICS
Advanced techniques

Views

1.4K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

cflib.org has a function called ListDistinct.  Get it.  Then build your list without worrying about duplicates and use this function when you are done.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

Thanks Dan. I didn't find it on the site you mentioned, but a Google search did take me to:

http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1003793#

Not sure if it's up to date, it was dated 2002. I took a look at the CFM that came with it,heres the code inside it, pretty much how I would have written it manually in CF, I think that it would possibly just use too many resources to keep looking over the list. Here's the code

<CFPARAM name="Attributes.list" default="">

<CFPARAM name="Attributes.return_vname" default="distinct_list">

<CFPARAM name="Attributes.sort" default="No">

<CFSET temp_list="">

<CFLOOP list="#Attributes.list#" index="i">

    <CFIF ListFindNoCase(temp_list,i) EQ 0>

        <CFSET temp_list = ListAppend(temp_list,i)>

    </CFIF>

</CFLOOP>

<CFIF Attributes.sort EQ "Yes">

    <CFSET "Caller.#Attributes.return_vname#" = ListSort(temp_list, "Text")>

<CFELSE>

    <CFSET "Caller.#Attributes.return_vname#" = temp_list>

</CFIF>

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

If you want to use it as it is, you'll also need teh ListSort function.  It's in the same library.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

Ah, I didn't notice that. So regardless of how I use it I presume I'll need that?

the instructions just had it set up as a custom tag

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 24, 2012 Mar 24, 2012

Copy link to clipboard

Copied

LATEST

ACS LLC, you could translate the custom tag into a function, something like this

<cffunction name="removeDuplicates" access="public" output="no" returntype="string" hint="This function will take a list and remove all duplicate values from it.">

<!--

Parameters:

list (required): input list

sort (optional): perform a text-sort on the list?  Yes or No. Default is No.

-->

<cfargument name="list" required="true" >

<cfargument name="sort" required="false" default="no">

<cfset var returnList = "">

<cfset var temp_list    = "">

<cfloop list="#arguments.list#" index="i">

    <cfif listfindnocase(temp_list,i) eq 0>

        <cfset temp_list = listappend(temp_list,i)>

    </cfif>

</cfloop>

<cfif arguments.sort eq "yes">

    <cfset returnList = listsort(temp_list, "text")>

<cfelse>

    <cfset returnList = temp_list>

</cfif>

<cfreturn returnList>

</cffunction>

<cfset testlist="a,a,1,1,2">

<cfoutput>#removeDuplicates(testlist)#</cfoutput>

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines