2011-06-16 103 views
2

可能重複:
Comparing two arrays & get the values which are not common從陣列的使用PowerShell的比較獲取缺少元素

我想邏輯從數組中得到罕見的物品,例如:

[email protected](1,2,3,4,5,6) 
[email protected](1,2,3,4,5,7,9,10) 

我希望輸出$c爲6,這是中缺少的元素數組,優先級只應該給予$a的數組內容。

任何人都可以請幫我解決這個問題嗎?
謝謝!

+0

檢查我在你前面的問題的評論,你會發現(有一點點的努力)。 – stej 2011-06-16 08:22:23

+0

嗨@stej im ujable讓它出來! – PowerShell 2011-06-16 08:58:56

+0

嗨@stej它不是一個重複的問題,我想優先給陣列$ a現在 – PowerShell 2011-06-16 09:00:54

回答

4

要麼EMPO的做法,或

[email protected](1,2,3,4,5,8) 
[email protected](1,2,3,4,5,6) 
Compare-Object $a1 $b1 | 
    Where-Object { $_.SideIndicator -eq '<=' } | 
    Foreach-Object { $_.InputObject } 

回報8

2
$c = $a | ? {!($b -contains $_)} 

優先級將賦予變量「管道」。

+1

您還可以使用:?{$ b -notcontains $ _} – Rynant 2011-06-16 12:06:17

+0

或'? {-not($ b -contains $ _)}',結果是一樣的。 – 2011-06-16 12:38:36