我有2個數組,每個數組有2個字段(例如'item'和'price')。在Powershell 2D陣列中查找缺失的行
以下是我的陣列(實際上是兩個數組具有相同的結構)
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
item NoteProperty System.String field1=computer
price NoteProperty System.String field2=2000
我需要找到的物品中找不到陣列$雪巴的項目1到Get-Member導致數組$ shopB。我現在使用2個循環找到缺失的項目。
$missing = @()
foreach ($itemA in $shopA) {
$found = 0
foreach ($itemB in $shopB) {
if ($itemB.item -eq $itemA.item) {
$found = 1
}
}
if ($found = 0) {
$missing += $itemA
}
}
此方法適用於我,但我的2個陣列是相當大的,我想比循環整個數組直通更快的方法...
我一直在尋找更好的方法來做到這一點和比較對象幾乎可以完成這項工作,但所有的例子似乎只適用於單維數組。
謝謝
謝謝喬伊。我想知道如果我可以解決這個問題而不用建立另一個散列表,但如果這是最好的方法,那麼我將創建一個散列表。 –
您提到大數組,因此從性能的角度來看,您應該創建用於檢查存在性的散列表。 – Joey
如果我需要創建一個新表,我還會嘗試使用compare-object。非常感謝,您的建議非常有幫助。 –