hashtable - Powershell v2.0 substitute null values from a Hash table -
i have hash table below:
$hash = @{ team1=$team1.count team2=$team2.count team3=$team3.count } $groupbyteam = new-object psobject -property $hash | select 'team1','team2','team3' | convertto-html -fragment
this fine , each "team" returns own value. however, teams may have null value , wish substitute "0".
in attempt work out, have tried select null value first can't seem this:
$hash.values | select -property values values ------ {1, 2}
but
$hash.values | select -property values | {$_.values $null}
doesn't pull anything. tried:
$hash.values | select -expandproperty values | {$_.values $null}
any ideas?
thanks
your best option cast values int
when creating hashtable:
$hash = @{ team1 = [int]$team1.count team2 = [int]$team2.count team3 = [int]$team3.count }
if that's not possible reason go enumerator:
($hash.getenumerator()) | foreach-object { if ($_.value -eq $null) { $hash[$_.name] = 0 } }
or (as mathias suggested) use keys
property same end:
($hash.keys) | foreach-object { if ($hash[$_] -eq $null) { $hash[$_] = 0 } }
note either way need use subexpression (or assign enumerated objects/keys variable) otherwise you'll error because you're modifying data structure while it's being enumerated.
Comments
Post a Comment