Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Support array (collection) slicing with all-remaining and except-last-N elements logic #7940
Note that a related array-slicing feature suggestion, #7928, does not require array awareness and could be implemented on the range operator (
# Sample array $a = 'one', 'two', 'three', 'four', 'five' # CURRENTLY required syntax for returning everything starting with the 3rd element: $a[2..($a.Count-1)] three four five # WISHFUL THINKING: having $a.Count-1 be *implied* as the end of the range. $a[2..] three four five
Aside from being more concise, this has the added advantage of not needing the input array to be stored in a variable beforehand.
# Sample array $a = 'one', 'two', 'three', 'four', 'five' # CURRENTLY required syntax for returning everything except the last 3 elements: $a[0..($a.Count-1 - 3)] one two # WISHFUL THINKING: allow specifying just N, without explicitly needing to refer # to the end of the array. # Note that $a[0..-3] does NOT work, because it creates array 0, -1, -2, which does something different. $a[0..@-3] one two
As stated, this would require introducing special syntax specific to collection indexing, and such modified range expressions wouldn't make sense outside that context.
I am not wedded to the specific syntax forms proposed above -
The less desirable alternative (from an end-user perspective) would be to introduce a new automatic variable representing the array's highest index, such as
Written as of:
PowerShell Core 6.1.0
History notes: When I first implemented ranges, I'd planned to support a unary range operator e.g.
I think @BrucePay indeed meant
Note that in the all-remaining scenario you can even currently get away without arithmetic, because it's benign (though potentially confusing) to exceed the array bounds by 1:
$a=1,2,3; $a[1..$a.Count] # works, though strictly speaking it should be `($a.Count - 1 )` 2, 3
Yes, the need for parentheses is unfortunate, but providing the automatic highest-index variable would indeed help (I suggested
That said, using the variable-less special syntax I proposed would make both problems go away (parentheses, need for new variable).
Are we open to special syntax in the context of indexing (
I think a lot of this is handled by
Being able to select arbitrary elements such as
What about leaving range
Except in Strict-Mode, then it's System.IndexOutOfRangeException
As a general rule, expression-mode solutions and pipeline solutions aren't interchangeable, for performance reasons.
My preference is to make do with minor tweaks to the existing range-operator syntax, to avoid confusion and reduce complexity (another thing to learn).
As an aside:
Good point, though, to be precise, it is
If I read the linked articles correctly (and keep in mind that things might change before release):
Some of what's coming to C# 8 has been a part of PowerShell since the beginning (kudos, PowerShell):
Here's what's new, which covers what this issue proposes with some extra syntactic sugar:
Important: In C# 8, array slices will actually refer to the returned elements in place, via
Syntax-wise, an option is therefore to go with