【问题标题】:Dynamically splitting an array in powershell在powershell中动态拆分数组
【发布时间】:2015-10-11 04:06:20
【问题描述】:

我正在尝试构建一个函数,它将字符串数组拆分为 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
    }

似乎出错的地方是“获取变量”部分。 Powershell报错:

Get-Variable:找不到名为“arrPartial8”的变量。在 行:1 字符:1 + 获取变量-名称“arrPartial$i”-ValueOnly + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (arrPartial8:String) [Get-Variable], I temNotFoundException + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVari 能命令

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

PS Variable:\> dir

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

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

【问题讨论】:

  • 我复制并粘贴了这个并针对我自己的数组运行并且没有出现这样的错误。您是否尝试过新的控制台?

标签: arrays powershell hashtable associative-array


【解决方案1】:

不确定我是否正确理解您要达到的目标。你想“折叠”一个一维数组吗

[ 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
}

【讨论】:

  • 您甚至可以使用[array]$ht["arrPartial$i"] += $_ 分配并完全跳过第一个循环
  • 感谢 Ansgar Wiechers!这正是我想要的:D
  • 抱歉,stackoverflow 新手。绝对解决了问题:)
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2020-06-06
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多