2012-03-14 104 views

回答

6

最簡單的我能想到的是

$array | %{ $_ + 1 } 

$_ + 1是我想要的轉變。

所以,你可以這樣做:

$new = $array | %{$_ + 1} 

$array = $array | %{ $_ + 1} 

PS: 如果你願意,可以定義地圖功能:

function map ([scriptblock]$script, [array] $a) { 

    $a | %{ & $script $_ } 

} 

map {param($i) $i + 1} 1,2,3 

或撰寫Sy的擴展方法stem.Array:http://blogs.msdn.com/b/powershell/archive/2008/09/06/hate-add-member-powershell-s-adaptive-type-system.aspx

+0

另外'$ anonymousFunc = {$ _ * 3}'和'1,2,3 | %$ anonymousFunc' – stej 2012-03-14 07:15:10

0

您也可以使用過濾器:

$arr = 1,2,3 

filter addone {$_ +1} 
$arr | addone 

或過濾功能:

function addone {process{$_ +1}} 
$arr | addone 

或匿名過濾器:

$addone = {$_ +1} 
$addone.isfilter = $true 
$arr | &$addone 

編輯:該匿名過濾器工作日可能會被編號。在V3測試版中不再有效,我懷疑這不會改變。看起來腳本塊在創建後立即編譯,並且.isfilter屬性在編譯後無法更改。

相關問題