1

For some reason PowerShell takes an array of strings and converts it into a single string (or perhaps a character array?) when it is set as the second dimension of a two dimensional array:

Here is the initial output of the $path array:

PS C:\Users\crd> $path
C:
Program Files (x86)
Common Files
Adobe
ARM
1.0
armsvc.exe

The path[0] array entry is a string value:

PS C:\Users\crd> $path[0]
C:

Now we set the first value in the $test[] array equal to our string array:

PS C:\Users\crd> $test[0]=$path

You can see that the entire string array has been merged:

PS C:\Users\crd> $test[0]
C: Program Files (x86) Common Files Adobe ARM 1.0 armsvc.exe

Why is this? I'm sure Microsoft documented this somewhere but I'm having a hard time finding it.

I would expect the output to remain unchanged. Note the following output:

PS C:\Users\chris> $test[0][0]
C

I would like this to be the same as the $path[0] output which was C:. Why is this merge occurring?

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
Shrout1
  • 2,497
  • 4
  • 42
  • 65
  • What would you expect it to set it to? – Sean Jun 14 '16 at 14:42
  • @Sean Revised my question a bit - hope that helps. – Shrout1 Jun 14 '16 at 14:51
  • 1
    Where does `$path` come from? What type is it? Why do you want to work with multi dimensional arrays? – Martin Brandl Jun 14 '16 at 15:09
  • 1
    What it `$test`, how is this different from [the question you asked yesterday](http://stackoverflow.com/questions/37795607/powershell-split-string-into-two-dimensional-array/37796098#37796098) and why do you keep wanting to re-appropriate and reuse the same variables? :) – Mathias R. Jessen Jun 14 '16 at 15:20
  • Why not use a hashtable or a powershell object and store the path a string then split it when you need to process? – jkdba Jun 14 '16 at 15:20
  • @MathiasR.Jessen This is different as yesterday I was successful in storing my data in a two dimensional array structure. This question is directly about why this behavior is occurring. – Shrout1 Jun 14 '16 at 15:35
  • @jkdba Hey! I am actually just curious as to why this is happening. I am indeed storing the string in a different data structure and will split it when needed. I just want to understand this! – Shrout1 Jun 14 '16 at 15:36
  • @Shrout1 I think you will need to declare your `$test` array. If it is not typed properly powershell will assume a string i believe. Try something like this `$Test = New-Object String[][] (3,3)`. Then you should be able to set like so `$Test[0] = $path`. – jkdba Jun 14 '16 at 15:46
  • @Shrout1 check out the second answer on this question: http://stackoverflow.com/questions/6157179/append-an-array-to-an-array-of-arrays-in-powershell – jkdba Jun 14 '16 at 15:50
  • @Shrout1 If we are to help you understand, please answer the question: What is `$test`? How did you declare/initialize/assign it before doing `$test[0] = $path`? – Mathias R. Jessen Jun 14 '16 at 15:56
  • @MathiasR.Jessen, he/she would have had to only declared it as so `$test=@()` if you dont this this `$test[0]=$path` would fail with unable to set index of null array. If he had gone further and declared as I mention in my comment above this would not be an issue. – jkdba Jun 14 '16 at 16:35
  • @jkdba What? Indexing into a zero-length array would surely throw an out of bounds exception. – Mathias R. Jessen Jun 14 '16 at 17:21
  • @MathiasR.Jessen correct, that is what I said. What I meant by "this would not be an issue" is the question that was asked, not the zero length array failure. – jkdba Jun 14 '16 at 17:22
  • @jkdba The only way he could end up with the behavior he sees, is if he has initialized `$test` with a `string[]` – Mathias R. Jessen Jun 14 '16 at 17:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114658/discussion-between-jkdba-and-mathias-r-jessen). – jkdba Jun 14 '16 at 17:26

1 Answers1

0

you changed your hash of items into a single string value. works as expected.

Kelly Davis
  • 354
  • 2
  • 6