【发布时间】:2011-01-28 14:57:57
【问题描述】:
我在 Powershell 中遇到了一个有趣的问题,但一直无法找到解决方案。当我用谷歌搜索(并找到像 this post 这样的东西)时,没有什么比我想做的更重要,所以我想我会在这里发布这个问题。
问题与外部数组长度为 1 的多维数组有关。看来 Powershell 非常坚持将像 @( @('A') ) 变成 @( 'A' ) 这样的数组扁平化。这是第一个sn-p(提示是>,顺便说一句):
> $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') )
有没有人遇到过这个问题,有没有人解决过这个问题?我知道我可以实例化框架对象,并且我假设如果我创建一个对象 []、一个列表 或其他类似的东西,一切都会好起来的,但我已经处理了一些事情确实似乎必须有正确的方法来做到这一点(不必实例化真正的框架对象)。
代码示例 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'
代码示例 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],因为数组中的逗号数量可能表明......事实证明,在这种情况下逗号不是元素分隔符。
标签: arrays powershell jagged-arrays