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".

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 4
    `return $array1` -> `return ,$array1` or `$array2 = testArray` -> `$array2 = @(testArray)` – user4003407 Oct 24 '16 at 14:11
  • @petseral it's works, any reason for that, or it's just syntax of powershell? – Benjamin TAN Oct 24 '16 at 14:14
  • 3
    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. – Ansgar Wiechers Oct 24 '16 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. – gravity Oct 24 '16 at 14:22

3 Answers3

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
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
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

saftargholi
  • 896
  • 1
  • 9
  • 25
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.

Basalt
  • 31
  • 2