将范围运算符与步骤配合使用 [英] Using range operator with a step

查看:104
本文介绍了将范围运算符与步骤配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell范围运算符生成值列表:

The PowerShell range operator generates a list of values:

>1..6

1
2
3
4
5
6

如何通过特定步骤生成值列表?例如,我需要第2步从1到10的列表.

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.

推荐答案

范围运算符本身不支持跳过/步进,但是您可以使用Where-Object(如果运行的是版本4.0,则可以使用Where()方法)或更高)以每秒过滤一次:

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

2.0及更高版本:

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

这篇关于将范围运算符与步骤配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆