How to compare two lists and find the differences between them?

The following function compares the new list of items with the old and finds the differences:

  • Items that have been removed from the old list
  • Items that have been added to the new list (not in the original list).

I wrote two loops for this, and they produced the following output:

oldItems = "an, old, list" ---> Items to delete: 'an, old'

newItems = "a, new, list" ---> Items to create: 'new'

The first problem: it ashould be displayed in the created elements, but I believe, because this is a substring anthat it does not receive.

The second problem (?) Is that I am doing two loops seems inefficient. Can I reorganize the code?

public function testList() hint="Compares two lists to find the differences."
{
    local.oldItems = "a, new, list";
    local.newItems = "an, old, list";
    local.toDelete = "";
    local.toCreate = "";

    // Loop over newItems to find items that do not exist in oldItems 
    for (local.i = 1; local.i LTE ListLen(local.newItems, ", "); local.i++)
    {
        if (! ListContains(local.oldItems, ListGetAt(local.newItems, local.i, ", ")))
        {
            local.toCreate = ListAppend(local.toCreate, ListGetAt(local.newItems, local.i, ", "));
        }
    }

    // Loop over old items to find items that do not exist in newItems
    for (local.i = 1; local.i LTE ListLen(local.oldItems, ", "); local.i++)
    {
        if (! ListContains(local.newItems, ListGetAt(local.oldItems, local.i, ", ")))
        {
            local.toDelete = ListAppend(local.toDelete, ListGetAt(local.oldItems, local.i, ", "));
        }
    }

    writeDump(var="Items To Delete: '" & local.toDelete & "'");
    writeDump(var="Items To Create: '" & local.toCreate & "'", abort=true);
}
+3
7

, , .

, ( ). , "a" .

, :

<cfscript>
    oldList = "an, old, list";
    newList = "a, new, list";

    result = compareLists(oldList, newList);

    writeDump(result);

    // -------------------------------------------------------------------------

    public struct function compareLists (
        required string listA,
        required string listB
    ){

        local.a = listToArray(arguments.listA, ',');
        local.b = listToArray(arguments.listB, ',');

        local.itemsNotInListB = [];
        local.itemsNewInListB = [];

        // Compare all items in 'list A' to 'list B'
        for (local.item in local.a) {
            if (!arrayContains(local.b, local.item))
            {
                arrayAppend(local.itemsNotInListB, local.item);
            }
        }
        // Compare all items in 'list B' to 'list A'
        for (local.item in local.b) {
            if (!arrayContains(local.a, local.item))
            {
                arrayAppend(local.itemsNewInListB, local.item);
            }
        }

        return {
             newItems = local.itemsNewInListB
            ,deletedItems = local.itemsNotInListB
        };
    }
</cfscript>
+5

ListFind() ListContains():

if (! ListFind(local.oldItems, ListGetAt(local.newItems, local.i, ", "), ", ")) {}

.

.

+3

3 UDF cflib.org:

- http://cflib.org/udf/listCompare - , , . , .

List Diff - http://cflib.org/udf/ListDiff - , .

Diff Dup - http://cflib.org/udf/ListDiffDup - , . ListDiff, .

+3

, . Java- , ColdFusion JVM:

<cfscript>
oldItems = "an, old, list"; //Items To Delete: 'an,old'
newItems = "a, new, list"; //Items To Create: 'a,new'
// ArrayList could be HashSet if items in both lists are expected to be unique
oldItems = createObject("java", "java.util.ArrayList").init(listToArray(oldItems, ", "));
newItems = createObject("java", "java.util.ArrayList").init(listToArray(newItems, ", "));

itemsToDelete = createObject("java", "java.util.HashSet").init(oldItems);
itemsToDelete.removeAll(newItems);

itemsToCreate = createObject("java", "java.util.HashSet").init(newItems);
itemsToCreate.removeAll(oldItems);
</cfscript>

<cfoutput>
itemsToDelete: #listSort(arrayToList(itemsToDelete.toArray()),"textNoCase")#<br /><!--- an,old --->
itemsToCreate: #listSort(arrayToList(itemsToCreate.toArray()),"textNoCase")#<!--- a,new --->
</cfoutput>

Java. .

+3

CFLib.org? , , . , ListContains. . ListFind ListFindNoCase.

+2
<cfset strListTupla = "SKU,CANTIDAD,VENTA">
<cfset strListSend = "CANTIDAD,SKU">

<cfset strListSend = ListSort(strListSend, "textnocase", "asc")>
<cfset strListTupla = ListSort(strListTupla, "textnocase", "asc")>

<cfset strListTupla = ListToArray (strListTupla)>
<cfloop index="j" from="1" to="#Arraylen(strListTupla)#">
    <cfoutput>
    <cfif NOT ListFind(strListSend, strListTupla[j])>
        Not Found #strListTupla[j]#
    </cfif><br/>
    </cfoutput>
</cfloop>

, , , , .

+1

2 , , . . ; β†’ . (Old < - New).

2 , , , .

I don’t know how good the for-loop is for this purpose, since local.i in the old list can be an index [7], and the same element that you need to check in the new can be an index [3] after possible changes.

0
source

Source: https://habr.com/ru/post/1784717/


All Articles