Steve B
Steve B

Reputation: 37660

Concatenating two arrays?

I'm struggling with a syntax issue to concatenate two arrays.

Basically, I have two arrays of string. The first contains several elements, the second only one (I use the , operator to create an array of string:

$array1= ,   "49e8e386-2f20-4139-ad61-a59500820afc", 
             "70a4fad9-5b8f-4750-9d6c-1e69400ed63b", 
             "1e368540-ebb3-4541-ab6e-78bcafaa6cf0"   
$array2=   , "1c1fcc2c-ccef-4898-9562-127b3f749830"  

I would like to merge the array in a new array of string, but it does not works as expected.

Specifically, I wrote :

$merged = $array1 + $array2

This seems to work, because I output $merged, I get:

49e8e386-2f20-4139-ad61-a59500820afc
70a4fad9-5b8f-4750-9d6c-1e69400ed63b
1e368540-ebb3-4541-ab6e-78bcafaa6cf0
1c1fcc2c-ccef-4898-9562-127b3f749830

But this is not an array of string:

PS C:\Users\steve> $merged | % { $_.GetType() }

IsPublic IsSerial Name     BaseType
-------- -------- ----     --------
True     True     Object[] System.Array
True     True     String   System.Object
True     True     String   System.Object
True     True     String   System.Object

As you can see, one of the value is a nested array, not a string. This is causing issues later in my scripts because of a string requirement.

What is to proper syntax to get an actual array of string?

Upvotes: 4

Views: 3705

Answers (1)

Steve B
Steve B

Reputation: 37660

Nevermind, the issue was related to the 1st array, and the , operator.

The correct syntax is :

$array1=     "49e8e386-2f20-4139-ad61-a59500820afc", 
             "70a4fad9-5b8f-4750-9d6c-1e69400ed63b", 
             "1e368540-ebb3-4541-ab6e-78bcafaa6cf0"   
$array2=   , "1c1fcc2c-ccef-4898-9562-127b3f749830" 

Actually, the array operator is required only when there is only one item in the array.

Upvotes: 0

Related Questions