从数组中删除空行

时间:2015-12-09 16:48:09

标签: arrays powershell

我有以下代码

//draws a circle
public static void drawCircle(Graphics g, int x, int y) {
}

//draws a cross
public static void drawCircle(Graphics g, int x, int y) {
}

//draws a basketball
public static void drawBasketball(Graphics g, int x, int y) {
  drawCircle(g, x, y);
  drawCross(g, x, y);
}

这有几个注意事项,首先在数组元素中有空行,我该如何删除它们?第二个在数组元素的末尾,我想添加另一行来计算元素中项目的总数。我有计数背后的代码,但添加到数组的最后一行是问题。

1 个答案:

答案 0 :(得分:0)

有几件事。除非您特别需要ArrayList,否则您可能只想坚持使用标准数组。它们更容易制作,更多人熟悉它们,因此您使用的任何问题都将更容易支持。至于删除空格,您可以使用Where语句。

$array = $array | Where{$_.'1 Security' -or $_.'1 Distro' -or $_.'2 Security' -or $_.'2 Distro' -or $_.'3 Security' -or $_.'3 Distro'} 

然后,您只需在末尾添加最后一个对象,其中包含每个属性的计数。

$Counts = [PSCustomObject]@{
                            '1 Security' = $array.'1 Security'.count
                            '1 Distro' = $array.'1 Distro'.count
                            '2 Security' = $array.'2 Security'.count
                            '2 Distro' = $array.'2 Distro'.count
                            '3 Security' = $array.'3 Security'.count
                            '3 Distro' = $array.'3 Distro'.count
                            }
$array += $Counts

因此改为使用标准数组而不是arraylist,并添加这些更改,整个事情看起来像这样:

ForEach($domain in ((Get-ADForest).Domains)){

    ForEach($group in (Get-ADGroup -Filter * -Properties Members -Server $domain | ? {-not $_.members})){

        [array]$array += [PSCustomObject] @{'1 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*1*"} | Select -ExpandProperty Name
                                   '1 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*1*"} | Select -ExpandProperty Name
                                   '2 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*2*"} | Select -ExpandProperty Name
                                   '2 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*2*"} | Select -ExpandProperty Name
                                   '3 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*3*"} | Select -ExpandProperty Name
                                   '3 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*3*"} | Select -ExpandProperty Name         
                                   }

    }

}

$array = $array | Where{$_.'1 Security' -or $_.'1 Distro' -or $_.'2 Security' -or $_.'2 Distro' -or $_.'3 Security' -or $_.'3 Distro'} 

$Counts = [PSCustomObject]@{
                            '1 Security' = $array.'1 Security'.count
                            '1 Distro' = $array.'1 Distro'.count
                            '2 Security' = $array.'2 Security'.count
                            '2 Distro' = $array.'2 Distro'.count
                            '3 Security' = $array.'3 Security'.count
                            '3 Distro' = $array.'3 Distro'.count
                            }
$array += $Counts
$array