CFLib.org – Common Function Library Project

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

Last updated August 20, 2001
Download UDF

author

Michael Slatoff                                   Michael Slatoff

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 1 time(s). Average Rating: 5.0

Description:
Returns elements in list1 that are found in list2. Based on ListCompare by Rob Brooks-Bilson (rbils@amkor.com) .

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>
#ListInCommon(FullList, PartialList, ";", ",", "|")#<BR>
#ListInCommon(FullList2, PartialList2)#<BR>
#ListInCommon(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:

<cfscript>
/**
* Returns elements in list1 that are found in list2.
* Based on ListCompare by Rob Brooks-Bilson (rbils@amkor.com)
*
* @param List1      Full list of delimited values.
* @param List2      Delimited list of values you want to compare to List1.
* @param Delim1      Delimiter used for List1. Default is the comma.
* @param Delim2      Delimiter used for List2. Default is the comma.
* @param Delim3      Delimiter to use for the list returned by the function. Default is the comma.
* @return Returns a delimited list of values.
* @author Michael Slatoff (rbils@amkor.commichael@slatoff.com)
* @version 1, August 20, 2001
*/

function ListInCommon(List1, List2)
{
var TempList = "";
var Delim1 = ",";
var Delim2 = ",";
var Delim3 = ",";
var i = 0;
// Handle optional arguments
switch(ArrayLen(arguments)) {
case 3:
{
Delim1 = Arguments[3];
break;
}
case 4:
{
Delim1 = Arguments[3];
Delim2 = Arguments[4];
break;
}
case 5:
{
Delim1 = Arguments[3];
Delim2 = Arguments[4];
Delim3 = Arguments[5];
break;
}
}
/* Loop through the second list, checking for the values from the first list.
* Add any elements from the second list that are found in the first list to the
* temporary list
*/

for (i=1; i LTE ListLen(List2, "#Delim2#"); i=i+1) {
if (ListFindNoCase(List1, ListGetAt(List2, i, "#Delim2#"), "#Delim1#")){
TempList = ListAppend(TempList, ListGetAt(List2, i, "#Delim2#"), "#Delim3#");
}
}
Return TempList;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson