3

I'm currently having an issue with a script that builds an array based off of a dynamic set of servers\priorities that are passed to it. For example, based off of the input below

server1,200
server2,200

I want to build something that looks like

$arr=@() #at this point I have an empty array
$arr+=@("server1",200) #at this point, I would expect to have an array
                       #that itself holds another array

However at this point, when I run the this I get unexpected output

echo $arr.count # result is 2, rather than the 1 I would expect
                # It appears to be treating $arr as a single array
                # with two members (server1 and 200) rather than an array
                # that holds an array, which itself has two members

However, if I add another empty array to my array:

$arr = @()
$arr += @()
$arr += $("server1",200)
$arr.count # output is 2, which is the desired result

I get my desired result. My question is.. can I get my desired result of a jagged\multidimensional array with just a single array inside of it? This isn't going to be a common scenario as a majority of the time there will be multiple sets of items that I'm dealing with, however I'd like to account for all scenarios and this may be one that pops up. I'd just like to not have to add an additional step of filtering out an empty array just to satisfy this.

Any input would be greatly appreciated.

1 Answer 1

4

Your 2 examples produce the exact same result, a flat array with 2 elements, because appending an empty array ($arr+=@()) does not modify the array in any way.

Demonstration:

PS C:\> $arr = @()
PS C:\> $arr += @('server1', 200)
PS C:\> $arr.Count
2
PS C:\> Format-Custom -InputObject $arr
server1
200
PS C:\> $arr = @()
PS C:\> $arr += @()
PS C:\> $arr += @('server1', 200)
PS C:\> $arr.Count
2
PS C:\> Format-Custom -InputObject $arr
server1
200

When you use the append operator (+=) with two arrays PowerShell basically concatenates the arrays. To append an array as a nested element to the first array you need to prepend the second array with the unary array construction operator (,).

PS C:\> $arr = @()
PS C:\> $arr += ,@('server1', 200)
PS C:\> $arr += ,@('server2', 200)
PS C:\> $arr.Count
2
PS C:\> Format-Custom -InputObject $arr

[
  server1
  200
]

[
  server2
  200
]

If you know the number of elements beforehand you could also pre-create an array of the desired size and assign the nested arrays as its elements. That doesn't require the comma operator.

PS C:\> $arr = New-Object Object[] 2
PS C:\> $arr.Count
2
PS C:\> $arr[0] = @('server1', 200)
PS C:\> $arr[1] = @('server2', 200)
PS C:\> Format-Custom -InputObject $arr

[
  server1
  200
]

[
  server2
  200
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.