Samstag, 16. Januar 2010

Eval-Min, Eval-Max, $null

When you want to determine the minimum or maximum of some property of all items in a pipeline, you can use Measure-Object.

But when you want to do some kind of performance monitoring, you want to eval the min or max so far, without stoping the running pipeline.

Primary to improve readability in my script I wrote the following two functions.

Attention I just found wrong results for negative arguments. I'm still checking. Look at the follow-ups.

function Eval-Max             
$max = $null
foreach ($i in $args){
if ($i -gt $max){
$max = $i

function Eval-Min
$min = $null
foreach ($i in $args){
if (($i -ne $null -and $i -lt $min) -or $min -eq $null){
$min = $i

Note that the two functions are not symmetric. Unlike SQL here in PowerShell we have

$null -lt 1            

$null -gt 1

PowerShell $null seems to have funny mathematical behaviour. It is less than 0 but greater than each negative number. In any case it is different from SQL NULL. be warned. Me functions here follow the SQL pattern. Therefore

Eval-Min $null 1            

Please if you know of earlier similar PowerShell functions drop a comment.

Keine Kommentare:

Kommentar veröffentlichen