[英]Powershell array containing array
我正在学习PowerShell(新手警报!!)并试图弄清楚为什么会出现以下奇怪的行为。 (环境:带有PowerShell 5的Windows 10)
C:\>POWERSHELL
Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.
PS > $A=(1,2,3) # When a variable stores a new array, ...
PS > $A # the elements are shown correctly.
1
2
3
PS > $B=$A # When the array is copied, ...
PS > $B # the elements are shown correctly.
1
2
3
PS > $B=($A,4,5,6) # When the variable stores a new array with elements from some other array ...
PS > $B # the elements are shown correctly.
1
2
3
4
5
6
PS > $B=($B,7,8,9) # But, when the variable stores a new array with elements from the array currently stored in the same variable, ...
PS > $B # something strange is seen
Length : 3
LongLength : 3
Rank : 1
SyncRoot : {1, 2, 3}
IsReadOnly : False
IsFixedSize : True
IsSynchronized : False
Count : 3
4
5
6
7
8
9
PS >
有关正在发生的事情的任何指示?
在输入这个问题时,我试图分析这种情况。 我看待它的方式:
$B=($A,4,5,6)
使$ B成为一个带数组元素的数组。
$B=($B,7,8,9)
使$ B成为一个带有数组元素的数组元素的数组。
显示变量内容的PowerShell CLI函数不会一直向下到叶元素,而是在第二级停止。
因此,最内部的数组(contents == $ A)显示为某个对象。
这个解释是否正确?
原因是PowerSer Just inflat one level。 所以,只需看看以下结果即可了解:
$B[0] -> 1,2,3,4,5,6
$B[0][0] -> 1,2,3 # Your $A
$b[0][0][0] -> 1 # Etc ...
$B[0][1] -> 4
$B[0][2] -> 5
$B[1] -> 7
$B[2] -> 8
$B[3] -> 9
如果你想要一个多维数组尽管有阵列数组,你可以使用:
$arrayAll = New-Object 'int[,]' (3,3)
$arrayAll[2,0] = 42
经过进一步分析(以及来自tire0011和Mathias R. Jessen和JPBlanc的输入)后,情况变得更加清晰。
这个问题分为两部分:
(A)数据是否以某种奇怪的格式存储(甚至被破坏)?
(B)如果没有,为什么输出没有显示从1到9的数字?
PS > ConvertTo-Json -InputObject $B
[
[
[
1,
2,
3
],
4,
5,
6
],
7,
8,
9
]
PS >
(A)数据正确存储在“数组内部数组”格式中,没有任何损坏。
(B)PowerShell CLI输出仅显示2个级别的内容,并显示更深层次的对象。 我找不到这个说法的参考,但我最接近的是: https : //technet.microsoft.com/en-us/library/hh849922.aspx
-Depth<Int32>
Specifies how many levels of contained objects are included in the JSON representation.
The default value is 2.
如果我在CLI输出中获得“深度”的引用,我将更新此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.