PowerShell数组方法之Foreach()与Where()

 1 <#
 2 1.PowerShell Array.Foreach(...)的签名是:
 3 Array Foreach(expression[, arguments...])
 4 第一个参数通常是ScriptBlock类型或者类型符号(表示类型转换)
 5 第二个以后的参数可以有很多个,都将作为$args自动变量的元素,传递给第一个参数
 6 
 7 2.PowerShell Array.Where(...)的签名是:
 8 Array Where({expression}[, mode[, numberToReturn]])
 9 mode=>[System.Management.Automation.WhereOperatorSelectionMode],默认值Default
10       【可以直接输入枚举名,PowerShell会自动转换,当然这是通用规则】
11 numberToReturn=>[int32],默认值0,表示返回所有匹配项
12 [System.Management.Automation.WhereOperatorSelectionMode]的所有枚举成员如下:
13 Default
14 Return all matches
15 返回所有匹配项
16 First
17 Stop processing after the first match.
18 返回最前的几个匹配项
19 Last
20 Return the last matching element
21 返回最后的几个匹配项
22 SkipUntil
23 Skip until the condition is true, then return the rest
24 忽略第一次匹配之前的所有项,并返回剩下的所有项,且包含第一匹配项
25 Split
26 Return an array of two elements, first index is matched elements, second index is the remaining elements.
27 返回一个只有两个元素的数组,第一个元素包含所有的匹配项,第二个元素包含所有的不匹配项
28 【此时参数numberToReturn只能决定匹配项的返回量,而因为numberToReturn而被抛弃的匹配项,将按在原数组中出现的顺序,被加入到不匹配项之中】
29 Until     
30 Return elements until the condition is true then skip the rest
31 返回第一个匹配之前的所有元素,不包含第一个匹配项
32 #>
33 Write-Host 1.1.取得一个等差数列
34 (1..10).ForEach({ $args[0] * $_ - $args[1] }, 2, 1)
35 Write-Host 1.2.取得所有的大写字母
36 (65..90).ForEach([char])
37 
38 Write-Host 2.1.筛选得到10以内的偶数
39 (1..10).Where({ $_ % 2 -eq 0 })
40 Write-Host 2.2.筛选得到10以内的偶数,但只取第一个
41 (1..10).Where({ $_ % 2 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::First)
42 Write-Host 2.3.筛选得到10以内的偶数,但只取前三个
43 (1..10).Where({ $_ % 2 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::First, 3)
44 Write-Host 2.4.筛选得到10以内的偶数,但只取最后一个
45 (1..10).Where({ $_ % 2 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::Last)
46 Write-Host 2.5.筛选得到10以内的偶数,但只取最后三个
47 (1..10).Where({ $_ % 2 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::Last, 3)
48 Write-Host 2.6.筛选得到10以内的第一个能被3整除的数之前的所有数字项
49 (1..10).Where({ $_ % 3 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::Until)
50 Write-Host 2.7.筛选得到10以内的第一个能被3整除的数之前的所有数字项中的前3个数字
51 (1..10).Where({ $_ % 3 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::Until, 3)
52 Write-Host 2.8.筛选得到10以内的第一个能被3整除的数之后的所有数字项,并包括第一个能被3整除的数字项
53 (1..10).Where({ $_ % 3 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::SkipUntil)
54 Write-Host 2.9.筛选得到10以内的第一个能被3整除的数之后的所有数字项,并包括第一个能被3整除的数字项,但只取前3个数字
55 (1..10).Where({ $_ % 3 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::SkipUntil, 3)
56 Write-Host 2.10.筛选以分别得到10以内的偶数与奇数项
57 (1..10).Where({ $_ % 2 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::Split)
58 Write-Host 2.11.筛选以分别得到10以内的偶数与奇数项,并只取匹配项的前3个,其余的数字全返回到不匹配项中
59 (1..10).Where({ $_ % 2 -eq 0 }, [System.Management.Automation.WhereOperatorSelectionMode]::Split, 3)
60 Write-Host 2.12.筛选得到10以内的偶数的前3项
61 (1..10).Where({ $_ % 2 -eq 0 }, 'default', 3)
原文地址:https://www.cnblogs.com/nutix/p/9016692.html