避免在Powershell中对不确定的锯齿数组进行扁平化

7
我在PowerShell中遇到了一个有趣的问题,但一直没有找到解决方法。当我搜索(并找到类似这篇文章)时,没有像我尝试做的那样涉及到这么多问题,所以我想在这里发布问题。
问题涉及到外部数组长度为1的多维数组。似乎PowerShell非常坚定地将数组扁平化,例如@( @('A') )变成了@( 'A' )。以下是第一个片段(提示符是>):
> $a =  @( @( 'Test' ) )
> $a.gettype().isarray
True
> $a[0].gettype().isarray
False

因此,我希望$a [0] .gettype().isarray为真,这样我就可以像$a [0] [0]一样索引值(实际情况是在循环内处理动态数组,并且我想要获取值作为$a [$i] [$j],但如果内部项不被识别为数组而被视为字符串(在我的情况下),则开始索引字符串的字符,如$a [0] [0] -eq 'T')。
我有几个长代码示例,因此我已将它们发布在末尾。并且,供参考,这是安装了PSv2和PSCX的Windows 7 Ultimate上。
考虑代码示例1:我使用+=运算符手动构建一个简单的数组。中间数组$w被展开,因此未正确添加到最终数组中。我已经在网上找到了类似问题的解决方案,基本上涉及在内部数组之前放置逗号以强制外部数组不展开,这确实有效,但再次说明,我正在寻找一种可以在循环内构建数组的解决方案(数组的嵌套数组,处理CSS文件),因此如果我将前导逗号添加到单个元素数组(作为中间数组$y实现),我希望对其他数组(如$z)执行相同的操作,但这会对将$z添加到最终数组产生负面影响。
现在考虑代码示例2:这更接近我遇到的实际问题。当从函数返回一个具有一个元素的多维数组时,它会被展开。在离开函数之前,它是正确的。再次说明,这些都是示例,我真正想要处理一个文件,而不必知道函数是否会返回@( @ ('color','black'))@( @ ('color','black'),@ ('background-color','white'))
有人遇到过这种情况吗?有人解决了这个问题吗?我知道我可以实例化框架对象,并且我假设如果我创建一个object [],list<>或类似的东西,一切都会很好,但是我已经处理了一段时间,并且某些事情似乎必须有正确的方法来做到这一点(而不必实例化真正的框架对象)。
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'

代码示例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'

感谢,马特。

在您的第一个代码示例中,不要使用$a = @( @( 'Test' ) ),而是使用$a = @( , @( 'Test' ) ),然后通过$a[0][0]尝试一下,看看PowerShell语法有多糟糕。添加单个“,”不仅改变了这种行为,而且元素仍然是[0][0],而不是数组中逗号数量所暗示的[1][0]。事实证明,在这种情况下,逗号不是元素分隔符。 - quetzalcoatl
这个回答解决了你的问题吗?只有一个元素的多维数组 - ggorlen
2个回答

8

还有一个问题也是从同样的问题开始:Powershell陷阱。看起来这是经过设计的。

我认为如果你返回,$ret而不是$ret,它应该可以工作。

另外两个注意事项:

  • 您可以通过$item -is [array]测试项目是否为数组(只是因为它看起来更像PowerShell ;))
  • @()仅对非数组项目有效。如果您链接@(@(@(1))),则将获得一个包含一个int项目的数组(@(@(@(1)))[0].gettype()返回Int32)。所以,@( ,@('r', 's') ),@('r', 's')相同。

0

我尝试了stej所说的方法,使用了这个例子:

function funC([int]$numOfPairs)
{
  $ret = @()
  if($numOfPairs -eq 1)
  { $ret = ,@('r','s') }
  elseif($numOfPairs -eq 2)
  { $ret = @('r','s'),@('t','u') }
  else
  { $ret = @('r','s'),@('t','u'),@('v','w') }

  Display $ret 0 "Inside Function C ($numOfPairs)"
  return ,$ret
}


### Start Program

$z = funC 1
Display $z 0 'Return from Function C(1)'
$z = funC 2
Display $z 0 'Return from Function C(2)'
$z = funC 3
Display $z 0 'Return from Function C(3)'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接