如何比较两个列表元素?

How to compare two list elements?

我有两个列表

<cfset thelist1 = valueList(Gettest.full_name) /> 
<cfset thelist2 =ReplaceNoCase(colorList2,".jpg","","all")>

thelist1 =(test1,test2,test3,test4,test5)
thelist2 = (test1,test3)

如何比较thelist1和thelist2,并从thelist1中获取不在thelist2中的元素?

我在想也许要获得不在 thelist2 上的列表,我必须创建另一个 thelist3。

运行 另一个循环中的嵌套循环。

<cfscript>
    theList1 = listToArray("1,2,3");
    theList2 = listToArray("2,3");
    notInList = "";
    for ( i=1 ; i<=arrayLen(theList1) ; i++ ) {
        itemFound = false;
        for ( ii=1 ; ii<=arrayLen(theList2) ; ii++ ) {
            if( theList1[i] EQ theList2[ii] ) {
                itemFound = true;
                break;
            }
        }
        if( !itemFound ){
            notInList = listAppend(notInList,theList1[i]);
        }
    }
</cfscript>

您会在列表 1 中找到列表 2 中没有的项目

我会使用一些 java 方法来做到这一点。这是我的工作:

<cfset theArray1 = listToArray(thelist1)>
<cfset theArray2= listToArray(thelist2)>

现在如果我想保留匹配项,那么我会这样做:

<cfset theArray1.retainAll(theArray2) />

如果我想删除匹配的项目,那么就像这样:

<cfset thearray1.removeAll(theArrar2) />

最后我会将数组转换为列表(如果需要的话)。

注意 removeAllretainAll 是 java 方法,但在 ColdFusion 中工作正常,您甚至不需要导入 java 库或包。

NewList 变量将包含 List1 元素,这些元素不在 List2

<cfset thelist1 = "test1,test2,test3,test4,test5">
<cfset thelist2 = "test1,test3">

<cfset NewList = ListRemoveDuplicates(thelist1)>

<cfloop list="#thelist2#" index="i">
    <cfif listFindNoCase(NewList, i)>
         <cfset NewList = listdeleteat(NewList,listFindNoCase(NewList, i))>
    </cfif>
</cfloop>

<cfdump var="#NewList#" />

另一种选择是使用闭包和标准列表函数生成第一个列表中包含的所有元素的列表(或数组),但不是第二个:

resultArray = [];
listEach(firstList, function(value, index) {
    if (!listFindNoCase(secondList, value)) {
        arrayAppend(resultArray, value);
    }
});

话虽如此,第一个列表的来源似乎是数据库查询。如果第二个项目列表相对较小,这也可以在您的数据库查询中完成。只需使用 WHERE NOT IN (...) 子句即可检索所提供列表中包含的所有 而非 的值。像这样:

QueryExecute("SELECT full_name FROM yourTable WHERE full_name NOT IN ( :filterList )"
    , { filterList={ value=secondList, cfsqltype="CF_SQL_VARCHAR", list="true"} }
);

函数 fLvL 到 return 数组 [onlyInFirst, onlyInSecond, inBoth]:

<cffunction name="fLvL" hint="function List vs List: returns array[listOnlyInFirst, listOnlyInSecond, listInBoth]">
    <cfargument name="argL1" default="">
    <cfargument name="argL2" default="">
    <cfargument name="argDelim" default=",">
    <cfargument name="argDedup" default="1" hint="boolean 0/1 for Y/N, also will work w true/false">
    <cfargument name="argSort" default="0" hint="boolean 0/1 for Y/N, also will work w true/false">
    <cfargument name="argSortType" default="textNoCase" hint="others: numeric, text (case sens), aabzABZ, aAaBbBzzZ">

    <cfset var raRet=["","",""]>
    <cfif !len(argL1)>
        <CFRETURN ["", argL2, ""]>
    <cfelseif !len(argL2)>
        <CFRETURN [argL1, "", ""]>
    </cfif>
    <cfloop index="iL1item" list="#argL1#">
        <cfif listFindNoCase(argL2, iL1item, argDelim)>
            <cfset raRet[3] = listAppend(raRet[3], iL1item, argDelim)>
        <cfelse>
            <cfset raRet[1] = listAppend(raRet[1], iL1item, argDelim)>
        </cfif>
    </cfloop>
    <cfloop index="iL2item" list="#argL2#">
        <cfif !listFindNoCase(argL1, iL2item, argDelim)>
            <cfset raRet[2] = listAppend(raRet[2], iL2item, argDelim)>
        </cfif>
    </cfloop>
    <cfif argSort>
        <cfloop index="iCnt" from="1" to="3">
            <cfset reRet[iCnt] = listSort(reRet[iCnt], argSortType, argDelim)>
        </cfloop>
    </cfif>
    <cfif argDedup>
        <cfloop index="iCnt" from="1" to="3">
            <cfset reRet[iCnt] = listRemoveDuplicates(reRet[iCnt], argDelim)>
        </cfloop>
    </cfif>
    <cfreturn raRet>
</cffunction>

<cfset raResult=fLvL("first,list","second,list")>
<cfdump var="#raResult#">
<!--- returns: ["first","second","list"] --->

raResult=fLvL("first,list","second,list") returns数组: ["first","second","list"]

还提供了一些进一步的参数选项(可能有点矫枉过正)。