PowerShellでは、パイプラインで受け取った値を処理する方法にbegin/process/endというものが存在します。
まずは、下記スクリプトを入力してください。
PS > function f >> { >> begin { $local:cnt = 1 } >> process { ("$local:cnt:$_"); $local:cnt += 1 } >> end { $local:cnt } >> } >>
このスクリプトには、begin/process/end がありますが、パイプラインから値を受け取ると
begin {最初の1回だけ実行}process {パイプラインで受け取った値ごとに実行}end {最後に1回だけ実行}
という動作をします。
つまり、入力していただいた関数f は
最初にローカル変数cntを1で初期化パイプラインから受け取った値を($_に値が代入されている)表示し、ローカル変数をインクリメント最後にローカル変数の現在値を表示
ということを行います。
前回のパイプライン引数(PowerShell)入門では、パイプライン引数の値は$input自動変数に入ることを説明しました。
process節でパイプラインの値を取得する場合は $_ 自動変数となりますので、使用には注意してください。
説明が長くなりましたが、実際にこの関数にパイプラインから値を渡してみましょう。
PS > "apple","banana","orange" | f 1:apple 2:banana 3:orange 4
パイプラインからの値処理で前処理と後処理を行いたい場合は、是非使用してみてください。
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail