简体   繁体   中英

Powershell combine the elements of two arrays into one another

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?

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

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()

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

If you want the elements to stay in the same order, just do $array3 = $array1 + $array2 . If you want to sort it though, do $array3 = ($array1 + $array2) | sort $array3 = ($array1 + $array2) | sort .

Here is a slightly modified version of Theos answer. Looks cleaner and its faster:

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

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

$arrayResult = @()
$arrayResult = foreach ($i in 0..$maxIndex) {
    if ($i -lt $len1) { $array1[$i] }
    if ($i -lt $len2) { $array2[$i] }
}
$arrayResult

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM