CFLib.org – Common Function Library Project

listGetDistinctValues(theList[, delimiter])

Last updated February 24, 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson