5

I encountered some issue in converting my existing vbs script to PowerShell script. I have illustrate here with some dummy codes instead of my original code. In example 1, I only have 1 set of elements in the array, upon return the array variable to the function, it will only display P.

However in example 2, where I have 2 set of elements in the array, upon return the array variable to the function, it will display the elements properly.

If you print the array inside the function in example 1 and 2. There isn't any issue in getting the results.

I have googled and not able to find any solution to it. Many thanks in advance for the kind help.

Example 1:

function testArray {
    $array1 = @()
    $array1 += ,@("Apple","Banana")

    return $array1
}
$array2 = testArray
Write-Host $array2[0][1]

Result is "P".

Example 2:

function testArray {
    $array1 = @()
    $array1 += ,@("Apple","Banana")
    $array1 += ,@("Orange","Pineapple")

    return $array1
}
$array2 = testArray
Write-Host $array2[0][0]

Result is "Apple".

4
  • 5
    return $array1 -> return ,$array1 or $array2 = testArray -> $array2 = @(testArray) Commented Oct 24, 2016 at 14:11
  • @petseral it's works, any reason for that, or it's just syntax of powershell? Commented Oct 24, 2016 at 14:14
  • 4
    PowerShell unrolls arrays returned from a function. By prepending the returned array with the array construction operator (,) you wrap it in another array, which is unrolled on return, leaving the nested array intact. Commented Oct 24, 2016 at 14:20
  • Is this a duplicate question? I'm sure this has to have been asked before on here. Either way, it's my first time seeing this issue myself, and am grateful I came across it. Kudos to you all. Commented Oct 24, 2016 at 14:22

3 Answers 3

9

PowerShell unrolls arrays returned from a function. Prepend the returned array with the comma operator (,, unary array construction operator) to wrap it in another array, which is unrolled on return, leaving the nested array intact.

function testArray {
    $array1 = @()
    $array1 += ,@("Apple","Banana")

    return ,$array1
}
Sign up to request clarification or add additional context in comments.

5 Comments

What about Write-Output -NoEnumerate $array1?
What about it? Does it have any advantages over return ,$array1 or just ,$array1?
Presumably, the advantage is that it doesn't create an additional array. The disadvantage is it's too verbose and long to type.
I would consider the overhead of creating an array with a single nested array reference negligible.
@wOxxOm Presumably, the advantage is that it doesn't create an additional array. Not true. It create additional array as well.
1

when you declare single line array like

$array1 = "Apple","Banana"

when you call :

$array1[0][1]

this will happen :

enter image description here

this code

function testArray {
    $array1 = @()
    $array1 += ,@("Apple","Banana")

    return $array1
}
$array2 = testArray
Write-Host $array2[0][1]

exact the same of this:

$array1 = "Apple","Banana"

but when you declare 2 row of array like :

function testArray {
    $array1 = @()
    $array1 += ,@("Apple","Banana")
    $array1 += ,@("Orange","Pineapple")

    return $array1
}
$array2 = testArray
Write-Host $array2[0][0]

this will happen :

enter image description here

if you need apple in your first code just call array[0] not array[0][0]. array[0][0] return char for you.

sorry for my bad english i hope you understand

Comments

0

$array1 inside your function TestArray is always of rank 1, fixed, and has never an element with type array. Check how a + is defined on arrays.

In this case you want to use an ArrayList: $array1=[Collections.ArrayList]::new(10) # 10 is capacity After that you can use the Add function. Even with arrays. And this time they will not be flattened, as was the case with the + array operator. Other dynamic collection types also exist. But this one looks ok for many purposes.

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.