在Powershell中避免不可知的锯齿状阵列扁平化

mlh*_*Dev 5 arrays powershell jagged-arrays

我在Powershell中遇到了一个有趣的问题,并且无法找到解决方案.当我谷歌(并找到像这篇文章的东西)时,没有任何涉及我正在尝试做的事情,所以我想我会在这里发布问题.

问题与外部数组长度为1的多维数组有关.似乎Powershell非常坚持像@( @('A') )变为扁平化阵列一样@( 'A' ).这是第一个片段(提示符是>,顺便说一句):

> $a =  @( @( 'Test' ) )
> $a.gettype().isarray
True
> $a[0].gettype().isarray
False
Run Code Online (Sandbox Code Playgroud)

所以,我希望$a[0].gettype().isarray是真的,所以我可以将值索引为$a[0][0](真实场景是在循环内处理动态数组,我想得到值$a[$i][$j],但如果内部项是不被识别为数组但作为字符串(在我的情况下),您开始索引字符串的字符,如在$a[0][0] -eq 'T').

我有几个很长的代码示例,所以我最后发布了它们.并且,作为参考,这是在安装了PSv2和PSCX的Windows 7旗舰版上.

考虑代码示例1:我使用+ =运算符手动构建一个简单的数组.中间阵列$w是扁平的,因此不会正确地添加到最终阵列中.我已经在网上找到类似问题的解决方案,基本上是在内部数组之前插入一个逗号来强制外部数组不会变平,这确实有效,但同样,我正在寻找一种可以在循环内构建数组的解决方案(一个锯齿状的数组数组,处理一个CSS文件),所以如果我将前导逗号添加到单个元素数组(实现为中间数组$y),我想对其他数组(如$z)做同样的事情,但这会产生负面影响如何$z添加到最终数组.

现在考虑代码示例2:这更接近我遇到的实际问题.当从函数返回具有一个元素的多维数组时,它将被展平.它离开函数之前是正确的.再一次,这些都是例子,我真的试图处理一个文件,而不必知道该函数是否会回来@( @( 'color', 'black') )或与@( @( 'color', 'black'), @( 'background-color', 'white') )

有没有人遇到这个,有没有人解决这个问题?我知道我可以实例化框架对象,并且我假设如果我创建一个对象[],列表<>或其他类似的东西,一切都会好的,但我一直在处理这个问题.肯定似乎必须有一个正确的方法来做到这一点(无需实例化真正的框架对象).

代码示例1

function Display($x, [int]$indent, [string]$title)
{
  if($title -ne '') { write-host "$title`: " -foregroundcolor cyan -nonewline }
  if(!$x.GetType().IsArray)
  { write-host "'$x'" -foregroundcolor cyan }
  else
  {
    write-host ''
    $s = new-object string(' ', $indent)
    for($i = 0; $i -lt $x.length; $i++)
    {
      write-host "$s[$i]: " -nonewline -foregroundcolor cyan
      Display $x[$i] $($indent+1)
    }
  }
  if($title -ne '') { write-host '' }
}


### Start Program
$final = @( @( 'a', 'b' ), @('c'))
Display $final 0 'Initial Value'

### How do we do this part ??? ###########
                                        ##
$w = @( @('d', 'e') )                   ##
$x = @( @('f', 'g'), @('h') )           ##
# But now $w is flat, $w.length = 2     ##
                                        ##
                                        ##
# Even if we put a leading comma (,)    ##
# in front of the array, $y will work   ##
# but $w will not. This can be a        ##
# problem inside a loop where you don't ##
# know the length of the array, and you ##
# need to put a comma in front of       ##
# single- and multidimensional arrays.  ##
$y = @( ,@('D', 'E') )                  ##
$z = @( ,@('F', 'G'), @('H') )          ##
                                        ##
                                        ##
##########################################

$final += $w
$final += $x
$final += $y
$final += $z
Display $final 0 'Final Value'


### Desired final value: @( @('a', 'b'), @('c'), @('d', 'e'), @('f', 'g'), @('h'), @('D', 'E'), @('F', 'G'), @('H') )
### As in the below:
# 
# Initial Value:
# [0]:
#  [0]: 'a'
#  [1]: 'b'
# [1]:
#  [0]: 'c'
# 
# Final Value:
# [0]:
#  [0]: 'a'
#  [1]: 'b'
# [1]:
#  [0]: 'c'
# [2]:
#  [0]: 'd'
#  [1]: 'e'
# [3]:
#  [0]: 'f'
#  [1]: 'g'
# [4]:
#  [0]: 'h'
# [5]:
#  [0]: 'D'
#  [1]: 'E'
# [6]:
#  [0]: 'F'
#  [1]: 'G'
# [7]:
#  [0]: 'H'
Run Code Online (Sandbox Code Playgroud)

代码示例2

function Display($x, [int]$indent, [string]$title)
{
  if($title -ne '') { write-host "$title`: " -foregroundcolor cyan -nonewline }
  if(!$x.GetType().IsArray)
  { write-host "'$x'" -foregroundcolor cyan }
  else
  {
    write-host ''
    $s = new-object string(' ', $indent)
    for($i = 0; $i -lt $x.length; $i++)
    {
      write-host "$s[$i]: " -nonewline -foregroundcolor cyan
      Display $x[$i] $($indent+1)
    }
  }
  if($title -ne '') { write-host '' }
}



function funA()
{
  $ret = @()
  $temp = @(0)
  $temp[0] = @('p', 'q')
  $ret += $temp
  Display $ret 0 'Inside Function A'
  return $ret # What about return ,$ret ? What about if $ret = @( @('p', 'q'), @('r', 's') ) -- would return ,$ret still work?
}

function funB()
{
  $ret = @( ,@('r', 's') )
  Display $ret 0 'Inside Function B'
  return $ret
}


### Start Program

$z = funA
Display $z 0 'Return from Function A'
$z = funB
Display $z 0 'Return from Function B'


### Desired final value: @( @('p', 'q') )  and same for r,s
### As in the below:
# 
# Inside Function A:
# [0]:
#  [0]: 'p'
#  [1]: 'q'
# 
# Return from Function A:
# [0]:
#  [0]: 'p'
#  [1]: 'q'
Run Code Online (Sandbox Code Playgroud)

谢谢,马特

ste*_*tej 6

另一个问题始于同一个问题:Powershell陷阱.看起来这是通过设计完成的.

我想如果你回来,$ret而不是$ret,它应该工作.

还有两个笔记:

  • 你可以测试,如果项是数组的$item -is [array](因为它看起来更像PowerShell;)
  • @()仅对非数组的项有效.如果你链接@(@(@(1))),你将得到一个带有一个int项的数组(@(@(@(1)))[0].gettype()返回Int32).
    所以,@( ,@('r', 's') )是一样的,@('r', 's').