CFLib.org – Common Function Library Project

listCompare(List1, List2 [, Delim1] [, Delim2] [, Delim3])

Last updated June 25, 2009
Download UDF

author

Rob Brooks-Bilson Rob Brooks-Bilson

Version: 2 | Requires: ColdFusion MX | Library: StrLib

 
Rated 9 time(s). Average Rating: 4.2

Description:
Compares one list against another to find the elements in the first list that don't exist in the second list. Performs the same funciton as the custom tag of the same name.

Return Values:
Returns a delimited list of values.

Example:

<CFSET FullList = "1;2;3;4;5;6;7;8;9;10">
<CFSET PartialList = "1,3,5,7,9">
<CFSET FullList2 = "1,2,3,4,5,6,7,8,9,10">
<CFSET PartialList2 = "1,3,5,7,9">
<CFSET FullList3 = "a,b,c,d,e,f,g">
<CFSET PartialList3 = "a,c">

<CFOUTPUT>
#ListCompare(FullList, PartialList, ";", ",", "|")#<BR>
#ListCompare(FullList2, PartialList2)#<BR>
#ListCompare(FullList3, PartialList3)#<BR>
</CFOUTPUT>

Parameters:

Name Description Required
List1 Full list of delimited values. Yes
List2 Delimited list of values you want to compare to List1. Yes
Delim1 Delimiter used for List1. Default is the comma. No
Delim2 Delimiter used for List2. Default is the comma. No
Delim3 Delimiter to use for the list returned by the function. Default is the comma. No

Full UDF Source:

<!---
Compares one list against another to find the elements in the first list that don't exist in the second list.
v2 mod by Scott Coldwell

@param List1      Full list of delimited values. (Required)
@param List2      Delimited list of values you want to compare to List1. (Required)
@param Delim1      Delimiter used for List1. Default is the comma. (Optional)
@param Delim2      Delimiter used for List2. Default is the comma. (Optional)
@param Delim3      Delimiter to use for the list returned by the function. Default is the comma. (Optional)
@return Returns a delimited list of values.
@author Rob Brooks-Bilson (rbils@amkor.com)
@version 2, June 25, 2009
--->

<cffunction name="listCompare" output="false" returnType="string">
<cfargument name="list1" type="string" required="true" />
<cfargument name="list2" type="string" required="true" />
<cfargument name="delim1" type="string" required="false" default="," />
<cfargument name="delim2" type="string" required="false" default="," />
<cfargument name="delim3" type="string" required="false" default="," />

<cfset var list1Array = ListToArray(arguments.List1,Delim1) />
<cfset var list2Array = ListToArray(arguments.List2,Delim2) />

<!--- Remove the subset List2 from List1 to get the diff --->
<cfset list1Array.removeAll(list2Array) />

<!--- Return in list format --->
<cfreturn ArrayToList(list1Array, Delim3) />
</cffunction>

Search CFLib.org


Latest Additions

Ryan Thompson-Jewell Ryan Thompson-Jewell added
ListSplit
3 day(s) ago

Nathan Dintenfass Nathan Dintenfass added
RowsToColumns
3 day(s) ago

Barney Boisvert Barney Boisvert added
indentXml
3 day(s) ago

Barney Boisvert Barney Boisvert added
REReplaceCallbac...
3 day(s) ago

Top Rated

Rob Brooks-Bilson                                 FolderSize
Rated 5.0, 7 time(s)

Nick Giovanni                                     UniqueValueList
Rated 5.0, 5 time(s)

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Jeff Howden ListDeleteDuplic...
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson