简体   繁体   English

包含数组的Powershell数组

[英]Powershell array containing array

I am learning PowerShell (newbie alert!!) and am trying to figure out why the following strange behavior is seen. 我正在学习PowerShell(新手警报!!)并试图弄清楚为什么会出现以下奇怪的行为。 (Environment : Windows 10 with PowerShell 5) (环境:带有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 >

Any pointers on what is going on ? 有关正在发生的事情的任何指示?

While typing this question, I was trying to analyze the situation. 在输入这个问题时,我试图分析这种情况。 The way I see it: 我看待它的方式:
$B=($A,4,5,6) makes $B an array with an array element. $B=($A,4,5,6)使$ B成为一个带数组元素的数组。
$B=($B,7,8,9) makes $B an array with an array element with an array element. $B=($B,7,8,9)使$ B成为一个带有数组元素的数组元素的数组。
The PowerShell CLI function which shows the variable contents, does not go all the way down to the leaf elements, but stops at the second level. 显示变量内容的PowerShell CLI函数不会一直向下到叶元素,而是在第二级停止。
Hence the inner most array (contents==$A) is shown as some object. 因此,最内部的数组(contents == $ A)显示为某个对象。
Is this explanation correct ? 这个解释是否正确?

The reason is that PowerSer Just inflat one level. 原因是PowerSer Just inflat one level。 So just have a look at the following results to understand : 所以,只需看看以下结果即可了解:

$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

If you want a multi-dimentional array in spite in spite of array of arrays, you can use : 如果你想要一个多维数组尽管有阵列数组,你可以使用:

$arrayAll = New-Object 'int[,]' (3,3)
$arrayAll[2,0] = 42

After further analysis (and inputs from tire0011 & Mathias R. Jessen & JPBlanc) the situation is becoming more clearer. 经过进一步分析(以及来自tire0011和Mathias R. Jessen和JPBlanc的输入)后,情况变得更加清晰。

There are two parts to this question : 这个问题分为两部分:
(A) Is the Data getting stored in some strange format (or even getting corrupted) ? (A)数据是否以某种奇怪的格式存储(甚至被破坏)?
(B) If not, why is the output not showing the numbers from 1 to 9 ? (B)如果没有,为什么输出没有显示从1到9的数字?

PS > ConvertTo-Json -InputObject $B
[
    [
        [
            1,
            2,
            3
        ],
        4,
        5,
        6
    ],
    7,
    8,
    9
]
PS >

(A) Data is correctly stored in "array inside array inside array" format, without any corruption. (A)数据正确存储在“数组内部数组”格式中,没有任何损坏。
(B) PowerShell CLI output shows content only 2 levels deep, and shows objects for deeper levels. (B)PowerShell CLI输出仅显示2个级别的内容,并显示更深层次的对象。 I could not find a reference for this claim, but the nearest I got was : https://technet.microsoft.com/en-us/library/hh849922.aspx 我找不到这个说法的参考,但我最接近的是: 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.

If I get a reference for "Depth" in the CLI output, I will update this answer. 如果我在CLI输出中获得“深度”的引用,我将更新此答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM