Skip to main content
Inspiring
March 10, 2012
Question

Checking for duplicates in a listappend

  • March 10, 2012
  • 1 reply
  • 1847 views

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

This topic has been closed for replies.

1 reply

Inspiring
March 11, 2012

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.

ACS LLCAuthor
Inspiring
March 11, 2012

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>

Inspiring
March 11, 2012

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