The following function compares a new list of items to an old one and finds the differences:
- Items that have been deleted from the old list
- Items that were added to the new list (not present in the original list).
I wrote two loops to achieve 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 issue is a
should show up in the items to create, but I believe because it's a substring of an
it's not getting picked up.
The second issue(?) is I doing two loops seems inefficient. Can the code be refactored?
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);
}
Best Answer
Yes, I believe you can refactor your code.
I prefer to use the array functions as it does an exact match (including case). This method ensures that "a" is picked up as a difference between the lists.
Hope this helps: