New UDF (makeDistinctList)

Posted by spiraldev, Posted on July 16, 2009

Quick and dirty UDF. I needed to make a list just have distinct values for logging purposes so I wrote a little UDF. Here it is

Update
After reading Adrian J. Moreno and doing some CFTimer testing I found that the code below night and day faster.

  1. Comments
  2. Tom de Manincor wrote on 07/16/09 11:32 AM

    You can save some resources by using a CF structure here. You cannot have duplicate struct key values. Which will allow you to eliminate the if-statement and extra list evaluations, speeding things up a bit.

    <cfscript>
          var i = 0;
          var tempStruct = structNew();
          
          for(i=1; i lte listLen(arguments.list,arguments.delimeter); i=i+1){
             tempStruct[listGetAt(arguments.list,i,arguments.delimeter)] = '';
          }
          
          return structKeyList(tempStruct,arguments.delimeter);   
       </cfscript>
    1. Comments
    2. Adrian J. Moreno wrote on 07/16/09 12:10 PM

      Actually, you can get the job done faster, especially for larger lists, by going directly to java:

      http://www.iknowkungfoo.com/blog/index.cfm/2008/10/22/Remove-duplicate-list-or-array-elements-using-ColdFusion-and-Java
      1. Comments
      2. spiraldev wrote on 07/16/09 12:32 PM

        ok I am sold on the the java route. I just created a list using cfloop
        <cfset form.SELECTED_STUDENTS = "3,2,1,2,3,4,1,5,6,7,3,3,9,0,3,2,0,3" />
        <cfloop from=1 to="500" index="i">
        <cfset form.SELECTED_STUDENTS = form.SELECTED_STUDENTS & "3,2,1,2,3,4,1,5,6,7,3,3,9,0,3,2,0,3" />


        and ran both sets of code and the winner is
        <cfset studentArray = listToArray( form.SELECTED_STUDENTS ) />
           <cfset lhs = createObject("java", "java.util.LinkedHashSet") />
           <cfset lhs.init( studentArray ) />
           <cfoutput>#arrayToList( lhs.toArray() )#</cfoutput>


        So thank you for pointing that out @Adrian J. Moreno.
        1. Comments
        2. watch one tree hill online wrote on 11/20/09 11:16 PM

          I will bookmark and continue reading your blog in the future! Thanks alot for the informative post!
          1. Comments
          2. CastanedaAlison25 wrote on 04/15/10 4:33 AM

            Have no cash to buy a building? You not have to worry, just because it's possible to get the <a href="http://lowest-rate-loans.com">loans</a>; to work out such kind of problems. Thence get a student loan to buy everything you want.
            1. Comments
            2. Ephedrine Weight Loss wrote on 08/25/10 2:31 PM

              I absolutely agree with the above said!
            3. Write your comment



              (it will not be displayed)