PowerShell - filtering for unique values
You might use a hash table:
$values = @(805265, 995874, 805674, 984654, 332574, 339852)
$ht = @{}
$values | foreach {$ht[$_ -replace '^(..).+','$1']++}
$ht.keys
99
98
33
80
I'd use the Group-Object
cmdlet (alias group
) for this:
Import-Csv foo.csv | group {$_.ColumnName.Substring(0, 2)} Count Name Group ----- ---- ----- 2 80 {805265, 805674} 1 99 {995874} 1 98 {984654} 2 33 {332574, 339852}
Use Select-Object
and parameter -unique
:
$values =
'805265',
'995874',
'805674',
'984654',
'332574',
'339852'
$values |
Foreach-Object { $_.Substring(0,2) } |
Select-Object -unique
If conversion to int is needed, then just cast it to [int]
:
$ints =
$values |
Foreach-Object { [int]$_.Substring(0,2) } |
Select-Object -unique