CFLib.org – Common Function Library Project

listGetDistinctValues(theList[, delimiter])

Last updated February 25, 2005

author

Dave Carabetta

Version: 1 | Requires: CF5 | Library: StrLib

Description:
This UDF takes a list that may have duplicate values in it and returns only the distinct values, either using the default comma delimiter or a delimiter of the user's choice that is passed in.

Return Values:
Returns a string.

Example:

<cfset variables.list = "BO,BO,AT,DC,BO,AT,HO,DC,DC,GA,VT" />
<cfoutput>distinct values with comma: #listGetDistinctValues(variables.list)#<br /></cfoutput>

<cfset variables.list = "BO.BO.AT.DC.BO.AT.HO.DC.DC.GA.VT" />
<cfoutput>distinct values: #listGetDistinctValues(variables.list,".")#</cfoutput>

Parameters:

Name Description Required
theList The list. Yes
delimiter List delimiter. Defaults to a comma. No

Full UDF Source:

/**
 * Returns a list of distinct values from a passed-in list.
 * 
 * @param theList      The list. (Required)
 * @param delimiter      List delimiter. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author Dave Carabetta (dcarabetta@gmail.com) 
 * @version 1, February 24, 2005 
 */
function listGetDistinctValues(theList) {
    var distinctValues = "";
    var totalValues = 0;
    var i = 0;
    var currentElement = "";
    var delimiter = ",";
    
    // If the user specifies their own delimiter, use that one instead
    if (arrayLen(arguments) GT 1) delimiter = arguments[2];
    
    totalValues = listLen( theList, delimiter );
    
    // Loops over each element in the original list and appends the current
    // element if it does not already exist in the distinct values list
    for (i=1; i LTE totalValues; i=i+1) {
        currentElement = listGetAt(theList, i, delimiter);
        if (not listFind(distinctValues, currentElement, delimiter) ) {
            distinctValues = listAppend(distinctValues, currentElement, delimiter);
        }
    }
    
    return distinctValues;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson