CFLib.org – Common Function Library Project

listsAreDistinct(list1, list2[, delim1][, delim2])

Last updated June 25, 2003

author

Nathan Dintenfass

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Given two lists, returns a boolean value telling you whether both lists have entirely different elements. In other words, no element in list 1 exists in list 2 and vice versa. The comparison is case insensitive.

Return Values:
boolean

Example:

<cfscript>
    list1 = "a,b,c,d,e,f";
    list2 = "g,h,i,j";
    list3 = "K+A+I";
</cfscript>

<cfoutput>
Is list1 (<em>#list1#</em>) distinct from list2 (<em>#list2#</em>)?
<br />
#yesNoFormat(listsAreDistinct(list1,list2))#
<br /><br />

Is list1 (<em>#list1#</em>) distinct from list3 (<em>#list3#</em>)?
<br />
#yesNoFormat(listsAreDistinct(list1,list3,",","+"))#
<br /><br />

Is list3 (<em>#list3#</em>) distinct from list2 (<em>#list2#</em>)?
<br />
#yesNoFormat(listsAreDistinct(list3,list2,"+"))#
</cfoutput>

Parameters:

Name Description Required
list1 The first list Yes
list2 The second list Yes
delim1 The delimiter of the first list (default is comma) No
delim2 The delimiter of the second list (default is comma) No

Full UDF Source:

/**
 * Tells whether two lists have entirely distinct elements
 * 
 * @param list1      The first list (Required)
 * @param list2      The second list (Required)
 * @param delim1      The delimiter of the first list (default is comma) (Optional)
 * @param delim2      The delimiter of the second list (default is comma) (Optional)
 * @return boolean 
 * @author Nathan Dintenfass (nathan@changemedia.com) 
 * @version 1, June 25, 2003 
 */
function listsAreDistinct(list1,list2){
    var delim1 = ",";
    var delim2 = ",";
    var ii = 0;
    var array = "";
    //deal with the optional delimiter arguments
    switch(arrayLen(arguments)) {
        case 3:
        {
            delim1 = arguments[3];
            break;
        }
        case 4:
        {
            delim1 = arguments[3];
            delim2 = arguments[4];
            break;
        }
    }
    //make list1 into an array for easy looping
    array = listToArray(list1,delim1);
    //loop through list1 checking for any matches in list2
    for(ii = 1; ii LTE arrayLen(array); ii = ii + 1){
        //if this element exists in list 2, return false
        if(listFindNoCase(list2,array[ii],delim2))
            return false;
    }
    //if we've gotten this far, the lists are distinct
    return true;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson