Home > Enterprise >  Place array into another array. [array that contains arrays]
Place array into another array. [array that contains arrays]

Time:10-21

I need to input two arrays and the output is an array that contains the two arrays.

Thank you for your time.

$firstArrayPair = @(1,2)
$secondArrayPair = @(1,4)


Function OutputNestedArray {
    Param
    (
    [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
    $arrayONE,
    [Parameter(Mandatory=$true,
                ValueFromPipelineByPropertyName=$true,
                Position=1)]
    $arrayTWO
    )

    $NewNestedArray = @()
    $NewNestedArray = $arrayONE   $arrayTWO
    return $NewNestedArray
}

$finalOutput = OutputNestedArray -arrayONE $firstArrayPair -arrayTWO $secondArrayPair

# The output needs to be an array containing 1 and 2.
# I can accept Arraylist or array

$finalOutput[0]

$finalOutput[0] : The output needs to be an array containing 1 and 2

CodePudding user response:

You don't need a separate function for this, you can use the , array operator directly for the exact same:

$finalOutput = $firstArrayPair,$secondArrayPair
$finalOutput[0] # this resolves to the same array as $firstArrayPair now

For future reference, you can suppress output enumeration with Write-Output -NoEnumerate:

return Write-Output $NewNestedArray -NoEnumerate

As an alternative, wrap the nested array in yet another array - PowerShell will then unroll the outer array you just created and leave the $NewNestedArray value untouched:

return ,$NewNestedArray
  • Related