user983965
user983965

Reputation: 1121

Powershell: multidimensional array changes return value from "system.object[]"

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.

Upvotes: 2

Views: 1183

Answers (3)

Matt
Matt

Reputation: 46710

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.

Upvotes: 2

Rafał Saltarski
Rafał Saltarski

Reputation: 707

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.

Upvotes: 0

Chris
Chris

Reputation: 1009

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.

Upvotes: 0

Related Questions