Update-List
在包含物件集合的屬性值中新增和移除項目。
Syntax
Update-List
[-Add <Object[]>]
[-Remove <Object[]>]
[-InputObject <PSObject>]
[[-Property] <String>]
[<CommonParameters>]
Update-List
-Replace <Object[]>
[-InputObject <PSObject>]
[[-Property] <String>]
[<CommonParameters>]
Description
Cmdlet Update-List
會在 物件的屬性值中新增、移除或取代專案,並傳回更新的物件。 此 Cmdlet 是專為包含物件集合的屬性所設計。
[新增] 和 [移除] 參數會將個別專案新增至集合,並將其從集合中移除。 Replace 參數會取代整個集合。
如果您未在命令中指定屬性, Update-List
則傳回描述更新的哈希表,而不是更新物件。 稍後,您可以使用此變更集來更新清單物件。
只有當要更新的屬性支援使用的 IList 介面 Update-List
時,此 Cmdlet 才能運作。 此外,接受更新的任何 Set
Cmdlet 都必須支援 IList 介面。
此 Cmdlet 已在 PowerShell 7 中重新引進。
範例
範例 1:將專案新增至屬性值
在此範例中,我們會建立一個類別,代表卡片的牌組儲存為 List 集合物件。 NewDeck () 方法會使用 Update-List
將完整的卡片值組新增至卡片集合。
class Cards {
[System.Collections.Generic.List[string]]$cards
[string]$name
Cards([string]$_name) {
$this.name = $_name
$this.cards = [System.Collections.Generic.List[string]]::new()
}
NewDeck() {
$_suits = "`u{2663}","`u{2666}","`u{2665}","`u{2660}"
$_values = 'A',2,3,4,5,6,7,8,9,10,'J','Q','K'
$_deck = foreach ($s in $_suits){ foreach ($v in $_values){ "$v$s"} }
$this | Update-List -Property cards -Add $_deck | Out-Null
}
Show() {
Write-Host
Write-Host $this.name ": " $this.cards[0..12]
if ($this.cards.count -gt 13) {
Write-Host (' ' * ($this.name.length+3)) $this.cards[13..25]
}
if ($this.cards.count -gt 26) {
Write-Host (' ' * ($this.name.length+3)) $this.cards[26..38]
}
if ($this.cards.count -gt 39) {
Write-Host (' ' * ($this.name.length+3)) $this.cards[39..51]
}
}
Shuffle() { $this.cards = Get-Random -InputObject $this.cards -Count 52 }
Sort() { $this.cards.Sort() }
}
注意
Cmdlet 會將 Update-List
更新的物件輸出至管線。 我們會使用管線將輸出傳送至 Out-Null
,以隱藏不必要的顯示。
範例 2:新增和移除集合屬性的專案
繼續進行範例 1 中的程式代碼,我們將建立 Card 類別的實例,以代表一組卡片和兩位玩家所持有的卡片。 Update-List
我們使用 Cmdlet 將卡片新增至玩家的手部,以及從牌組中移除卡片。
$player1 = [Cards]::new('Player 1')
$player2 = [Cards]::new('Player 2')
$deck = [Cards]::new('Deck')
$deck.NewDeck()
$deck.Shuffle()
$deck.Show()
# Deal two hands
$player1 | Update-List -Property cards -Add $deck.cards[0,2,4,6,8] | Out-Null
$player2 | Update-List -Property cards -Add $deck.cards[1,3,5,7,9] | Out-Null
$deck | Update-List -Property cards -Remove $player1.cards | Out-Null
$deck | Update-List -Property cards -Remove $player2.cards | Out-Null
$player1.Show()
$player2.Show()
$deck.Show()
Deck : 4♦ 7♥ J♦ 5♣ A♣ 8♦ J♣ Q♥ 6♦ 3♦ 9♦ 6♣ 2♣
K♥ 4♠ 10♥ 8♠ 10♦ 9♠ 6♠ K♦ 7♣ 3♣ Q♣ A♥ Q♠
3♥ 5♥ 2♦ 5♠ J♥ J♠ 10♣ 4♥ Q♦ 10♠ 4♣ 2♠ 2♥
6♥ 7♦ A♠ 5♦ 8♣ 9♥ K♠ 7♠ 3♠ 9♣ A♦ K♣ 8♥
Player 1 : 4♦ J♦ A♣ J♣ 6♦
Player 2 : 7♥ 5♣ 8♦ Q♥ 3♦
Deck : 9♦ 6♣ 2♣ K♥ 4♠ 10♥ 8♠ 10♦ 9♠ 6♠ K♦ 7♣ 3♣
Q♣ A♥ Q♠ 3♥ 5♥ 2♦ 5♠ J♥ J♠ 10♣ 4♥ Q♦ 10♠
4♣ 2♠ 2♥ 6♥ 7♦ A♠ 5♦ 8♣ 9♥ K♠ 7♠ 3♠ 9♣
A♦ K♣ 8♥
輸出會顯示牌牌的狀態,再將牌牌解除給玩家。 您可以看到每位球員都收到來自牌牌的五張牌。 最後的輸出會顯示牌牌在處理球員之後的狀態。 Update-List
用來從牌組選取卡片,並將其新增至球員的集合。 然後,玩家的卡片會使用 Update-List
從牌牌中移除。
範例 3:在單一命令中新增和移除專案
Update-List
可讓您在單一命令中使用 Add 和 Remove 參數。 在此範例中,Player 1 想要捨棄 4♦
和 , 6♦
並取得兩個新的卡片。
# Player 1 wants two new cards - remove 2 cards & add 2 cards
$player1 | Update-List -Property cards -Remove $player1.cards[0,4] -Add $deck.cards[0..1] | Out-Null
$player1.Show()
# remove dealt cards from deck
$deck | Update-List -Property cards -Remove $deck.cards[0..1] | Out-Null
$deck.Show()
Player 1 : J♦ A♣ J♣ 9♦ 6♣
Deck : 2♣ K♥ 4♠ 10♥ 8♠ 10♦ 9♠ 6♠ K♦ 7♣ 3♣ Q♣ A♥
Q♠ 3♥ 5♥ 2♦ 5♠ J♥ J♠ 10♣ 4♥ Q♦ 10♠ 4♣ 2♠
2♥ 6♥ 7♦ A♠ 5♦ 8♣ 9♥ K♠ 7♠ 3♠ 9♣ A♦ K♣
8♥
範例 4:將變更集套用至清單物件
如果您未指定屬性, Update-List
則傳回描述更新的哈希表,而不是更新物件。 您可以將哈希表轉換成 System.PSListModifier 物件,並使用 ApplyTo()
方法將變更集套用至清單。
$list = [System.Collections.ArrayList] (1, 43, 2)
$changeInstructions = Update-List -Remove 43 -Add 42
$changeInstructions
Name Value
---- -----
Add {42}
Remove {43}
([PSListModifier]($changeInstructions)).ApplyTo($list)
$list
1
2
42
參數
-Add
指定要新增至集合的屬性值。 以值應顯示在集合中的順序輸入值。
Type: | Object[] |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-InputObject
指定要更新的物件。 您也可以使用管線將物件更新為 Update-List
。
Type: | PSObject |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Property
指定包含要更新之集合的屬性。 如果您省略此參數, Update-List
會傳回代表變更的物件,而不是變更物件。
Type: | String |
Position: | 0 |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Remove
指定要從集合中移除的屬性值。
Type: | Object[] |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Replace
指定新的集合。 此參數會按其指定的項目來取代原始集合中的所有項目。
Type: | Object[] |
Position: | Named |
Default value: | None |
Required: | True |
Accept pipeline input: | False |
Accept wildcard characters: | False |
輸入
您可以使用管線將物件更新至此 Cmdlet。
輸出
根據預設,此 Cmdlet 會傳回描述更新的哈希表。
當您指定 Property 參數時,這個 Cmdlet 會傳回更新的物件。