Using a literal array of indices as an index expression breaks in method calls.
It looks like the , that's part of the array literal is misconstrued as a method-argument separator.
Steps to reproduce
[string]::join(' ', (0, 1, 2)[0, 1])
Expected behavior
Actual behavior
At line:1 char:32
+ [string]::join(' ', (0, 1, 2)[0, 1])
+ ~
Missing ']' after array index expression.
At line:1 char:35
+ [string]::join(' ', (0, 1, 2)[0, 1])
+ ~
Missing ')' in method call.
At line:1 char:35
+ [string]::join(' ', (0, 1, 2)[0, 1])
+ ~
Unexpected token ']' in expression or statement.
At line:1 char:36
+ [string]::join(' ', (0, 1, 2)[0, 1])
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndSquareBracket
To make the command work, you currently have to enclose either the entire expression in (...):
[string]::join(' ', ((0, 1, 2)[0, 1])) # OK
or just the array inside [...]:
[string]::join(' ', (0, 1, 2)[(0, 1)]) # OK
or use a variable:
$a = 0, 1; [string]::join(' ', (0, 1, 2)[$a]) # OK
Also, a single index works as expected:
[string]::join(' ', (0, 1, 2)[0]) # OK
Using the -join operator makes the problem go away:
(0, 1, 2)[0, 1] -join ' ' # OK
In short: avoiding a , in the index expression is needed to make the problem go away; such a , appears to be mistaken for a method-argument separator.
Environment data
PowerShell Core v6.1.0-preview.2 on macOS 10.13.4
PowerShell Core v6.1.0-preview.2 on Ubuntu 16.04.4 LTS
PowerShell Core v6.1.0-preview.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Windows PowerShell v5.1.17134.48 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Using a literal array of indices as an index expression breaks in method calls.
It looks like the
,that's part of the array literal is misconstrued as a method-argument separator.Steps to reproduce
Expected behavior
Actual behavior
To make the command work, you currently have to enclose either the entire expression in
(...):or just the array inside
[...]:or use a variable:
Also, a single index works as expected:
Using the
-joinoperator makes the problem go away:In short: avoiding a
,in the index expression is needed to make the problem go away; such a,appears to be mistaken for a method-argument separator.Environment data