2

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

2
  • 1
    what is is? It's not defined as a function or alias anywhere in your script, yet you call @(is ) Commented Oct 20, 2021 at 17:20
  • is was a typo. fixed code. Commented Oct 20, 2021 at 17:26

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.