can anyone explain the why the the 1st example gets different results than the following 2

Posted by klumsy on Stack Overflow See other posts from Stack Overflow or by klumsy
Published on 2011-03-13T05:55:08Z Indexed on 2011/03/13 8:10 UTC
Read the original article Hit count: 204

Filed under:
$b = (2,3)

$myarray1 = @(,$b,$b)

$myarray1[0].length #this will be 1
$myarray1[1].length

$myarray2 = @(
,$b
,$b
)

$myarray2[0].length #this will be 2
$myarray[1].length

$myarray3 = @(,$b
,$b
)

$myarray3[0].length #this will be 2
$myarray3[1].length

UPDATE

I think on #powershell IRC we have worked it out, Here is another example that demonstrates the danger of breaking with the comma on the following line rather than the top line when listing multiple items in an array over multiple lines.

$b = (1..20)

$a = @( $b, $b ,$b,
        $b, $b ,$b)

for($i=0;$i -lt $a.length;$i++)
{
  $a[$i].length
}        
"--------"
$a = @( $b, $b ,$b
       ,$b, $b ,$b)

for($i=0;$i -lt $a.length;$i++)
{
  $a[$i].length
}        

produces

20
20
20
20
20
20
--------
20
20
20
1
20
20

I'm curious how people will explain this. I think i understand it now, but would have trouble explaining it in a concise understandable fashion, though the above example goes somewhat towards that goal.

© Stack Overflow or respective owner

Related posts about powershell