2

I wondered if anyone could shed some light on the issue I am facing when returning values from a multidimensional array through a function:

$ArrayList = @()

function MultiDimensionalArrayTest
{
    param(
        [array]$ArrayList
    )
    for($i = 0; $i -lt 1; $i++)
    {
                $ArrayModify += ,@("Test", "Test")
    }

    return $ArrayModify

}

$ArrayModify = MultiDimensionalArrayTest

foreach ($item in $ArrayModify)
{
    Write-Host $item[0]
}

When the loop is executed once the values returned are:

T
T

However if the for statement is lopped twice, the values returned are:

Test
Test

My aim is to retrieve x amount of values "Test" from the Write-Host $item[0] regardless of how many times the statement is executed

It appears that if two or more rows are captured and returned in the $ArrayModify array, the value is a system.object[], however if looped once, the value "Test, Test" is captured and when Write-Host $item[0] is executed, it will print T.

Any advice would be greatly appreciated.

5
  • You either return an array or strings or an array of arrays. What's the confusion here? Commented Mar 13, 2015 at 11:45
  • Can you give further explanation of what is the global goal of your script? Commented Mar 13, 2015 at 12:03
  • @EtanReisner The OP wants to know what in his first result $ArrayModify is not getting a array with 1 element. That element being another array. As it stands he is getting an array with two elements which explains the output as you know. Commented Mar 13, 2015 at 14:30
  • @Matt Right but hat's just powershell unrolling the array on return I believe. Which was my point originally (though looking again I didn't make that at all clear). Commented Mar 13, 2015 at 15:14
  • @EtanReisner I knew that as well. Couldnt figure out a nice way to prevent that. Commented Mar 13, 2015 at 15:17

3 Answers 3

2

Not the cleanest way of dealing with it but you need to prevent PowerShell from unrolling the single element array into an array with two elements.

function MultiDimensionalArrayTest{
    $ArrayModify = @()

    for($i = 0; $i -lt 1; $i++){
        $ArrayModify += ,@("Test$i", "Test$i$i")
    }

    ,@($ArrayModify)

}

Using the above function will get the desired output I believe. ,@($ArrayModify) ensures that the array is returned and not unrolled into its elements as you saw above.

$ArrayList = @()
$ArrayList = MultiDimensionalArrayTest

foreach ($item in $ArrayList){$item[0]}

Giving the output for $i -lt 1 in the loop

Test0

Giving the output for $i -lt 2 in the loop

Test0
Test1

Your Output

Concerning your output from your example with $i -lt 1 PowerShell is unrolling the array into a single dimension array with 2 elements "Test" and "Test". You are seeing the letter "T" since all strings support array indexing and will return the character from the requested position.

Other issues with code

Not to beat a dead horse but really look at the other answers and comments as they provide some tips as to some coding errors and anomalies of the code you presented in the question.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you need that if test at all? The multi-array case is also being unrolled normally but then just getting rolled back up. Doesn't just using the ,@($ArrayModify) return always work (since unrolling doesn't stop at one level)?
@EtanReisner In practice if you do that it will always return a single element array which is not what the op wants. Was the first thing I tried. Will double check just in case... well crap it does. I swear that is was not working before. Thanks for making sure I didnt look like a complete idiot.
0

First of all I notice several mistakes in your code.

This line $ArrayList = @() is useless since you don't use the $ArrayList variable afterwards.

Regarding the MultiDimensionalArrayTest function, you declared an $ArrayList argument but you never use it in the function.

Finally when this line makes no sense $ArrayModify = MultiDimensionalArrayTest = $item, you probably meant $ArrayModify = MultiDimensionalArrayTest.

1 Comment

True, that's why I asked (in a comment) for further explanation of the global goal of the script so hopefully I will be able to properly answer.
0

This is not a complete answer to your question, since I am not sure what you are trying to achieve, but take a look at this line:

$ArrayModify = MultiDimensionalArrayTest = $item

This makes no sense, since you are calling the function MultiDimensionalArrayTest and passing two arguments to it, which are "=" (powershell assumes this is a string) and $item (null object). Then you assign whatever is returned to $ArrayModify.

The reason "T" is outputted, is because you are outputting the first element of what is at the moment a string. "Test" is outputted when $item is an array of strings.

1 Comment

The question is why is he getting the array of strings in the loop-once case but an array of arrays in the loop-twice case. Which is just normal annoying bizarre powershell array unrolling I believe.

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.