Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the idiomatic way to slice an array relative to both of its ends?

Powershell's array notation has rather bizarre, albeit documented, behavior for slicing the end of arrays. This section from the official documentation sums up the bizarreness rather well:

Negative numbers count from the end of the array. For example, "-1" refers to the last element of the array. To display the last three elements of the array, type:

$a[-3..-1] 

However, be cautious when using this notation.

$a[0..-2] 

This command does not refer to all the elements of the array, except for the last one. It refers to the first, last, and second-to-last elements in the array.

The following code confirms the bizarreness:

$a = 0,1,2,3 $a[1..-1] 

Which indeed outputs this bizarre result:

1 0 3 

So, the question is, what is the idiomatic way to slice with one index relative the start and another relative the end of the array?

Please tell me it's something better than this ugly mess:

$a[1..($a.Count-1)] 

Edit:

Another way to describe what I'm looking for is this: The idiomatic Powershell equivalent of this python expression:

a=1,2,3,4 a[1:-1] 

Which, of course, evaluates to (2,3)

like image 461
alx9r Avatar asked Feb 11 '15 17:02

alx9r


People also ask

What is the meaning of slicing an array?

In computer programming, array slicing is an operation that extracts a subset of elements from an array and packages them as another array, possibly in a different dimension from the original.

How do you create an array in Powershell?

To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma and separated from the variable name by the assignment operator ( = ). The comma can also be used to initialize a single item array by placing the comma before the single item.


2 Answers

If you want to get n elements from the end of an array simply fetch the elements from -n to -1:

 PS C:\> $a = 0,1,2,3 PS C:\> $n = 2 PS C:\> $a[-$n..-1] 2 3 

Edit: PowerShell doesn't support indexing relative to both beginning and end of the array, because of the way $a[$i..$j] works. In a Python expression a[i:j] you specify i and j as the first and last index respectively. However, in a PowerShell .. is the range operator, which generates a sequence of numbers. In an expression $a[$i..$j] the interpreter first evaluates $i..$j to a list of integers, and then the list is used to retrieve the array elements on these indexes:

 PS C:\> $a = 0,1,2,3 PS C:\> $i = 1; $j = -1 PS C:\> $index = $i..$j PS C:\> $index 1 0 -1 PS C:\> $a[$index] 1 0 3 

If you need to emulate Python's behavior, you must use a subexpression:

 PS C:\> $a = 0,1,2,3 PS C:\> $i = 1; $j = -1 PS C:\> $a[$i..($a.Length+$j-1)] 1 2 
like image 198
Ansgar Wiechers Avatar answered Oct 22 '22 01:10

Ansgar Wiechers


Although not as neat as you might want but is cleaner in the way PowerShell works ...

(@(1,2,3,4)) | Select-Object -Skip 1 

returns ...

2 3 4 
like image 39
John Warde Avatar answered Oct 22 '22 02:10

John Warde