Powershell combine the elements of two arrays into one another

13,289

Solution 1

try something like this

$array1 = (0,2,4)
$array2 = (1,3,5)

$MaxLen=[Math]::Max($array1.Length, $array2.Length)

$Result=@()

for ($i = 0; $i -lt $MaxLen; $i++)
{ 
    $Result+=$array1[$i]
    $Result+=$array2[$i]
}

$Result

Solution 2

Although Esperento57 gives you a perfect working solution, here's my idea that will also allow for arrays that are not of the same length. It uses a System.Collections.ArrayList to add the values from the arrays for better performance if you have large arrays to combine.

$array1 = (0,2,4)
$array2 = (1,3,5,6,7,8)

$len1 = $array1.Length
$len2 = $array2.Length
$maxLength = [Math]::Max($len1, $len2)

$listResult = New-Object System.Collections.ArrayList
for ($i = 0; $i -lt $maxLength; $i++) {
    if ($i -lt $len1) { [void] $listResult.Add($array1[$i]) }
    if ($i -lt $len2) { [void] $listResult.Add($array2[$i]) }
}

$listResult.ToArray()

Solution 3

here's another way to do it. [grin]

this one takes into account dissimilar sizes in the arrays and interleaves them until one array runs out of items. the remaining items in the larger array are then added without "ghost" items from the the smaller array.

$array1 = @(0,2,4)
$array2 = @(5,7,9,11)

$InterleavedArray = [System.Collections.Generic.List[int]]::new()

$UpperBound = [math]::Max($array1.GetUpperBound(0), $array2.GetUpperBound(0))

foreach ($Index in 0..$UpperBound)
    {
    if ($Index -le $array1.GetUpperBound(0))
        {
        $InterleavedArray.Add($array1[$Index])
        }
    if ($Index -le $array2.GetUpperBound(0))
        {
        $InterleavedArray.Add($array2[$Index])
        }
    }

$InterleavedArray

output ...

0
5
2
7
4
9
11

hope that helps,
lee

Share:
13,289

Related videos on Youtube

secondplace
Author by

secondplace

Updated on June 04, 2022

Comments

  • secondplace
    secondplace almost 2 years

    I'd like to join two arrays by picking the elements from each array one by one. and not have them combined or simply merged

    I know how to add a second array to the first one as in:

    $array1 = (0,4,8)
    $array2 = (1,5,2)
    $array1 += $array2
    $array1
    

    Which results in the following:

    0
    4
    8
    1
    5
    2
    

    But how can I copy them into one another giving me an output like this:

    0
    1
    4
    5
    8
    2
    

    Note: I don't want to merge them and then sort the list.

    The elements need to stay in the same order. How would that be achieved?

    • iRon
      iRon over 5 years
      Just to clarify; if the arrays are:$array1 = (0,2,4) and $array2 = (5,3,1), the expected result is 0 5 2 3 4 1?
    • secondplace
      secondplace over 5 years
      @iRon yes, that is correct.
  • secondplace
    secondplace over 5 years
    sorting is not an option as i noted in my first post, i know how to do that. i need the elements copied one after another from each array.
  • secondplace
    secondplace over 5 years
    I'm sure this will be handy some day, thank you. I'n my current situation the arrays correlate with each other and have always the same length.
  • secondplace
    secondplace over 5 years
    by changing the [int] to [object] this will also work with text input. thank you.
  • Lee_Dailey
    Lee_Dailey over 5 years
    @secondplace - yes, the type can be most anything. you are most welcome! glad to have helped! [grin] take care,
  • LeeM
    LeeM over 3 years
    As a note, what you get is two arrays inside another array. Which may have unintended results if you've combined 2 arrays of 2 strings, you're doing a foreach over your new array, and you're expecting to loop through 4 strings. You're going to loop through 2 objects instead.