Using range operator with a step

别来无恙 提交于 2020-02-03 10:19:19

问题


The PowerShell range operator generates a list of values:

>1..6

1
2
3
4
5
6

How can I generate a list of values with a specific step? For example, I need a list from 1 to 10 with step 2.


回答1:


The range operator itself doesn't support skipping/stepping, but you could use Where-Object (or the Where() method if you're running version 4.0 or above) to filter out every second:

PS C:\> (1..10).Where({$_ % 2 -eq 0})
2
4
6
8
10

Version 2.0 and up:

PS C:\> 1..10 |Where-Object {$_ % 2 -eq 0}
2
4
6
8
10


来源:https://stackoverflow.com/questions/33892174/using-range-operator-with-a-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!