4

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?

Steve B
  • 36,818
  • 21
  • 101
  • 174

1 Answers1

0

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.

Steve B
  • 36,818
  • 21
  • 101
  • 174
  • This answer makes no sense to me. So I have downvoted it. I don't understand the significance of the snippet as applied to the question and how does this solve the concatenation problem. – Armen Michaeli Feb 21 '20 at 14:48
  • @amn: `$merged = $array1 + $array2` will contains actually five elements, which is the expected result. Thanks to the updated syntax. What's missing for your understanding ? ? – Steve B Feb 24 '20 at 08:59
  • 1
    I searched for "array concatenation in Powershell" and stumbled upon your question. Upon reading it, I confirmed for myself your question effectively calls for a correct and idiomatic solution to concatenating arrays in Powershell, and so I scrolled for answers. The only answer is yours. But it doesn't communicate clearly to me how to concatenate arrays or what was even wrong -- how that comma affects what. It just specifies the presumably correct syntax for defining multiple arrays. Sitting with Powershell now to see what's going on here. – Armen Michaeli Feb 24 '20 at 13:57
  • @amn I was confused too. I think it's b/c Steve is using `,` before `$array1` where he didn't need to. He's using `,` before the single element in `$array2` to make an array from a single object, but the `,` in the first makes element 0 an array of a string and then null? (`Write-Host ($merged[0][1] -eq $null)` gives $True) Better would be to say `$array2= @("1c1fcc2c-ccef-4898-9562-127b3f749830")` & to use `@()` in both. You're right: The question title does not reflect the title, & search engines pointing here are doing searchers a disservice. **The issue is declaration, not concatenation.** – ruffin Jan 11 '22 at 05:08
  • Looks like my `@()` suggestion was wrong -- there are times with strings where you simply cannot create the intended single element array w/out [the comma operator](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.2&viewFallbackFrom=powershell-6#comma-operator-) or your array will "[unroll](https://stackoverflow.com/a/1829401/1028230)". More good info at [this answer](https://stackoverflow.com/a/53427301/1028230). Luckily still right that the take-away remains that the issue described here is declaration, not concatenation. – ruffin Jan 11 '22 at 14:19