在 powershell 中动态拆分数组

Dynamically splitting an array in powershell

我正在尝试构建一个将字符串数组拆分为 a) 具有 [x] 个字符串数组的哈希表 - 或 - b) x 个字符串数组

我现在得到的是:

Function Split-Array {
    Param (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [array]$arrToSplit,

        # Param2 help description
        [int]$SplitInTo = 8
    )

        $Round = 0 
        $hashSplitted = @{}

        For ($i = 0 ; $i -le ($SplitInTo -1) ; $i++ ) {
            New-Variable -Name "arrPartial$i" -Value @()
            }

        While (($Round * $SplitInTo) -le $arrToSplit.Count) {
            For ($i = 0 ; $i -le ($SplitInTo - 1) ; $i++) {
                $arrDynamicVariable = Get-Variable -name "arrPartial$i" -ValueOnly
                $arrDynamicVariable += $arrToSplit[($Round * $SplitInTo) + $i]
                Set-Variable -Name "arrPartial$i" -Value $arrDynamicVariable
                }
            $Round++
            }

        For ($i = 0 ; $i -le ($SplitInTo -1) ; $i++) {
            $hashSplitted[$i] = Get-Variable -Name "arrPartial$i" -ValueOnly
            }
        $hashSplitted
    }

似乎出错的地方是 "Get-Variable" 部分。 Powershell 报错:

Get-Variable : Cannot find a variable with the name 'arrPartial8'. At line:1 char:1 + Get-Variable -name "arrPartial$i" -ValueOnly + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (arrPartial8:String) [Get-Variable], I temNotFoundException + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVari ableCommand

奇怪的是,似乎创建了 arrPartial 变量,但它与例如声明的变量有点不同。 "$arra = @()",如下图:

PS Variable:\> dir

Name                           Value                                                 
----                           -----                                                 
$                              )                                                     
?                              True                                                  
^                              $arra                                                 
args                           {}                                                    
arra                           {}                                                    
arrPartial0                   {}                                                    
arrPartial1                   {}                                                    
arrPartial2                   {}                                                    
arrPartial3                   {}                                                    
arrPartial4                   {}                                                    
arrPartial5                   {}                                                    
arrPartial6                   {}                                                    
arrPartial7                   {}                                 

请注意 arrPartialx 数组的 {} 向左缩进。这是一个错误还是我在这里做错了什么?欢迎任何关于不同方法的想法。

不确定我是否正确理解了您要实现的目标。是否要"fold"一维数组

[ a, b, c, d, e, f, g, h, i, j ]

进入这样的哈希表(为简单起见假设 $SplitInTo = 3)?

{
  arrPartial0 => [ a, d, g, j ]
  arrPartial1 => [ b, e, h ]
  arrPartial2 => [ c, f, i ]
}

如果是这样,那么您的方式太复杂了。这样的东西就足够了:

function Invoke-FoldArray {
  Param(
    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [array]$arrToSplit,
    [int]$SplitInTo = 8
  )

  $ht = [ordered]@{}

  0..($SplitInTo-1) | % {
    $ht["arrPartial$_"] = @()
  }

  $i = 0
  $arrToSplit | % {
    $ht["arrPartial$i"] += $_
    $i++
    $i %= $SplitInTo
  }

  return $ht
}