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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM