Cole9350
Cole9350

Reputation: 5570

pass Multidimensional array to a function

I have a multidimensional array of integers that only works inside of the function, but produces random numbers when I try to pass it to another function.

PS> $array
1
2
3
4
PS> $array[0]
1
2
PS> $array[0][1]
2

The array was originally formed with:

$array = @(($data1),($data))

and appended on $data for any array after that: ($data is cleared each iteration)

$array += ,($data)

Which seems to be fine, considering before I tried to create functions everything was working. I then try to pass the array into a another function

theFunc ($array)
    Function theFunc {
    param ($theData)
    #process data
    }

I'm getting the correct ammount of arrays, but the numbers inside them do not match the multidimensional array that I passed in one bit. Any help is appreciated, thank you.

Upvotes: 2

Views: 2372

Answers (1)

Klark
Klark

Reputation: 8280

I tried the following example and everything seems to be fine:

$data1 = @(1, 2, 3, 4)
$data2 = @(5, 6, 7, 8)
@($data1, $data2)
$array[0]
1
2
3
4
$array[1]
5
6
7
8
function myFunc { param($arr) $arr; $arr[0]; $arr[1]; }
myFunc($array)
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8

Please provide output you get.

Upvotes: 1

Related Questions