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

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

Raymond Compton Raymond Compton added
structBlend
20 day(s) ago

Duncan Duncan added
IsZIPUK
20 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
26 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson