以下功能項目的一個新的列表與舊的和發現的差異:已經從舊的列表中刪除添加到新名單 如何比較兩個列表並找出它們之間的差異?
- 項目(不存在的原始列表)。
我寫了兩個循環來實現這一點,他們產生了以下的輸出:
oldItems = "an, old, list" ---> Items To Delete: 'an,old'
newItems = "a, new, list" ---> Items To Create: 'new'
第一個問題是a
應在項目創建露面,但我相信,因爲它的一個子an
它沒有被拾起。
第二個問題(?)是我做兩個循環似乎效率低下。代碼可以重構嗎?
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);
}
亞倫,這很棒,謝謝。我認爲這對我來說將會非常有用。然而,就我的目的而言,我認爲我將使用一個循環,並使用Ray提到的ListFind()函數。我認爲你的UDF很棒,但在這種情況下這有點矯枉過正。感謝演示! – Mohamad 2011-01-11 13:17:04
不用擔心。但是,我確實回答了你的問題:) – 2011-01-11 13:44:34