简体   繁体   中英

Powershell array containing array

I am learning PowerShell (newbie alert!!) and am trying to figure out why the following strange behavior is seen. (Environment : Windows 10 with PowerShell 5)

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=($B,7,8,9) makes $B an array with an array element with an array element.
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.
Hence the inner most array (contents==$A) is shown as some object.
Is this explanation correct ?

The reason is that 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.

There are two parts to this question :
(A) Is the Data getting stored in some strange format (or even getting corrupted) ?
(B) If not, why is the output not showing the numbers from 1 to 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.
(B) PowerShell CLI output shows content only 2 levels deep, and shows objects for deeper levels. I could not find a reference for this claim, but the nearest I got was : 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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