Search code examples
coldfusioncoldfusion-9

How to compare two list elements?


I have two list

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

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

How can I compare thelist1 to thelist2 and get the elements that are not in thelist2 from thelist1?

I was thinking maybe to get the list that is not on thelist2 I have to create another thelist3.


Solution

  • I would use some java methods to do that. Here is what I do:

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

    Now if I want to retain matching items, then I would do something like this:

    <cfset theArray1.retainAll(theArray2) />
    

    And if I want to remove the matching items, then some thing like this:

    <cfset thearray1.removeAll(theArrar2) />
    

    Then finally I would convert the array to a list (if it is required) .

    Note removeAll and retainAll are java methods, but work fine in ColdFusion and you don't even need to import the java library or package.