2

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.

3
  • Can you provide more info. What kind of output do you get? Commented Jul 16, 2012 at 21:04
  • Turns out the problem lies in how I am clearing the second array each time, which doesn't really make sense considering I had it working at one point but: '$data = $data.Clear' was seeming to break the array, changing it to a single variable, and '$data.clear' doesn't actually clear the array Commented Jul 17, 2012 at 13:58
  • Problem solved: to give you a little insight incase your interested. I had a double loop, on the inside loop it would collect the data and put it in the first array. On the outer loop it would take that array and push it into the multidimensional array. The array was then attempting to be cleared but incorrectly. I fixed this problem by reinstantiating the array each time $data = @() and that worked better than any clear function. Thank you for your help Commented Jul 17, 2012 at 14:08

1 Answer 1

1

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.

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.