CFLib.org – Common Function Library Project

IsListInList(l1, l2 [, delim1] [, delim2] [, matchany])

Last updated September 4, 2008
Download UDF

author

Daniel Chicayban Daniel Chicayban

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

 
Rated 4 time(s). Average Rating: 3.3

Description:
If ALL elements of a list X is found in a list Y, the function returns TRUE (yes) otherwise it'll return FALSE (no). Also allows for 'matchany' functionality which will allow for any match of X in Y.

Return Values:
Returns a boolean.

Example:

<cfset l1 = "apples,oranges">
<cfset l2 = "apples,bananas,oranges">
<cfset l3 = "apples@zebras">
<cfoutput>
Is all of #l1# in #l2#? #isListInList(l1,l2)#<br>
Is all of #l1# in #l3#? #isListInList(l1,l3,",","@")#<br>
</cfoutput>

<cfif isListInList("sm,m,l,xl","l,xl,sm",",",",",true)>
Show Prodcut
<cfelse>
Don't show product
</cfif>

Parameters:

Name Description Required
l1 The first list. Yes
l2 The second list. UDF checks to see if all of l1 is in l2. Yes
delim1 List delimiter for l1. Defaults to a comma. No
delim2 List delimiter for l2. Defaults to a comma. No
matchany If true, UDF returns true if at least one item in l1 exists in l2. Defaults to false. No

Full UDF Source:

<cfscript>
/**
* Checks is all elements of a list X is found in a list Y.
* v2 by Raymond Camden
* v3 idea by Bill King
* v4 fix by Chris Phillips
*
* @param l1      The first list. (Required)
* @param l2      The second list. UDF checks to see if all of l1 is in l2. (Required)
* @param delim1      List delimiter for l1. Defaults to a comma. (Optional)
* @param delim2      List delimiter for l2. Defaults to a comma. (Optional)
* @param matchany      If true, UDF returns true if at least one item in l1 exists in l2. Defaults to false. (Optional)
* @return Returns a boolean.
* @author Daniel Chicayban (dbastos@math.utoledo.edu)
* @version 4, September 4, 2008
*/

function isListInList(l1,l2) {
    var delim1 = ",";
    var delim2 = ",";
    var i = 1;
    var matchany = false;
    
    if(arrayLen(arguments) gte 3) delim1 = arguments[3];
    if(arrayLen(arguments) gte 4) delim2 = arguments[4];
    if(arrayLen(arguments) gte 5) matchany = arguments[5];
    
    for(i=1; i lte listLen(l1,delim1); i=i+1) {
        if(matchany and listFind(l2,listGetAt(l1,i,delim1),delim2)) return true;
        if(not matchany and not listFind(l2,listGetAt(l1,i,delim1),delim2)) return false;
    }
    return not matchany;
}
</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