考慮這個哈希表陣列(編輯為來自@mclayton 的功能修復):
$some_keys= @(
@{
foo="one";
bar="some other stuff";
},
# (...more entries...)
@{
foo="two";
bar="some more stuf";
}
)
另外,請考慮以下功能:
function Get-WithKeys($foo, $bar){
# do stuff with $foo, $bar
}
Powershell 是否提供任何慣用的管道方式,some_keys以便Get-WithKeys每個陣列元素呼叫一次,并將哈希圖的鍵作為函式引數傳遞?
出于說明目的,我想知道 Powershell 是否為這種效果提供了一些東西:
$some_keys | <magic step> | Get-WithKeys
uj5u.com熱心網友回復:
它被稱為“噴濺” - 請參閱about_Splatting。
基本上,您使用哈希表變數作為引數呼叫該函式,但在其前面加上前綴,并且哈希表@中$的每個鍵都被視為命名引數。
一個簡單的例子:
$my_hashtable = @{
foo="one";
bar="some other stuff"
}
# equivalent to Get-WithKeys -foo "one" -bar "some other stuff"
Get-WithKeys @my_hashtable
在您的原始示例中,您可以這樣做以呼叫陣列Get-WithKeys中的每個專案一次:$some_keys
$some_keys | foreach-object { Get-WithKeys @_ }
在哪里@_使用自動變數 $_作為變數來splat。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/412538.html
標籤:
