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

前端 未结 7 1428
梦如初夏
梦如初夏 2021-02-06 20:35

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

7条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 21:26

    Combine Select-Object -Skip and Select-Object -SkipLast like:

    $a = 0,1,2,3
    $a | Select-Object -Skip 1 | Select-Object -SkipLast 1
    

    Returns:

    1
    2
    

    Not as elegant as Python, but at least you don't have to use Count or Length, meaning this also works if the array isn't stored in a variable.

提交回复
热议问题