Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Multidimensional Array From Function

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

like image 444
Benjamin TAN Avatar asked Oct 24 '16 14:10

Benjamin TAN


People also ask

Can a function return a 2D array?

A whole array cannot be provided as a parameter to a function in C++.

How is array returned from a function?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

Can function return type array?

We can also make a function return an array by declaring it inside a structure in C++. Let us see how. Here, note that we have declared the array arr inside the structure demo . And this time the function has a return type of the structure itself and return demo_mem (structure variable) instead of the array.

Can we return 2D array in C++?

We can have a pointer to the two-dimensional arrays in C++. We can pass a 2D array to a function by specifying the size of columns of a 2D array. We can also pass a pointer to an array. We can also define a pointer as a pointer to pass in a function.


2 Answers

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
}
like image 174
Ansgar Wiechers Avatar answered Oct 28 '22 11:10

Ansgar Wiechers


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

like image 20
saftargholi Avatar answered Oct 28 '22 11:10

saftargholi