Powershell: range operator

Created on 1 Mar 2020  路  7Comments  路  Source: PowerShell/PowerShell

range operator in powershell is not very powerfull like other languages it great to add to this range operator some feature like

$a = 1..10
$a[::] # all element
$a[1..^0] # 2 to end 
$a[^1] # last element 10
$a[^3] # element 8
$a[^2..0] # element 9 to 1
Issue-Enhancement Resolution-Duplicate WG-Language

Most helpful comment

It's simply that the range operator functions _everywhere_, not just with respect to an array. Having 1..-4 work the same way regardless of where it's used means you know what you're going to get, and you can inspect what's going on when you get unexpected results by simply displaying the result of 1..-4 -- if it changed behaviour when used in an array or other indexer, it would be impossible to reliably inspect or diagnose an issue with any accuracy.

C#8 introduced a new operator ^ which is specifically used to create a separate Index and Range object types that is designed to work relative to collections. We could borrow that part of the approach and have a syntax like this: $a[0..^4] which would _always_ go until the 4th-from-the-end index in the array, whatever numbered index that might be.

Whether we need to introduce a new object type for this, or just reuse the new Index and Range types now available in .NET Core, this would be a valuable addition to the language and I believe has been discussed before... in #7940

All 7 comments

It is already here

$a # all element
$a[-1] # last element 10
$a[-3] # element 8

Adding to what @scriptingstudio said we have some of the sub-range operators already, like this:

```powershell
PS C:\foo> $a = 1,2,43,4,5,6,7,87,389
PS C:\foo> $a[1..2]
2
43
PS C:\foo> $a[-2..-1]
87
389

Seems to me we have sufficient range operators.

@doctordns I limited my example to 3 cases because array range calculation is not perfect:

$a = 1..10
$a[0..-4]
1
10
9
8
7

It makes sence to improve range calculation, does not it?

Current behavior is documented but looks suddenly.

It's simply that the range operator functions _everywhere_, not just with respect to an array. Having 1..-4 work the same way regardless of where it's used means you know what you're going to get, and you can inspect what's going on when you get unexpected results by simply displaying the result of 1..-4 -- if it changed behaviour when used in an array or other indexer, it would be impossible to reliably inspect or diagnose an issue with any accuracy.

C#8 introduced a new operator ^ which is specifically used to create a separate Index and Range object types that is designed to work relative to collections. We could borrow that part of the approach and have a syntax like this: $a[0..^4] which would _always_ go until the 4th-from-the-end index in the array, whatever numbered index that might be.

Whether we need to introduce a new object type for this, or just reuse the new Index and Range types now available in .NET Core, this would be a valuable addition to the language and I believe has been discussed before... in #7940

also one other idea is add step

"a".."z"..2

1..100..5

and padding with zero

001..200

display 001,002...etc instead 1,2,...

This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings