我正在嘗試了解以下輸出和錯誤訊息:
# declare empty array
$thisDataArray = @()
# assign array values
$thisDataArray = '123','cadabra','456','789','trouble'
# declare empty array to receive specific values
$letters = @()
# use this pattern to filter array members that contain letters
$regex = "[a-z]"
# confirm the array values
Write-Host $thisDataArray
# pipe array & filter to find the values
$thisDataArray | Select-String -AllMatches -Pattern $regex -CaseSensitive | ForEach-Object { $letters = $_.Matches.Value}
# output the result
Write-Host $letters
錯誤是:變數$letters已分配但從未使用過。輸出是:
123 cadabra 456 789 trouble
t r o u b l e
我的問題是:
- 怎么
Write-Host $letters不使用分配的變數$letters? - 為什么我只能從我的 ? 中得到一個陣列成員“麻煩”
$regex? - 最后。為什么麻煩的字符之間有空格,例如“麻煩 e”
任何建議表示贊賞。
uj5u.com熱心網友回復:
- 如何
Write-Host $letters不使用分配的變數$letters?
因為它在ForEach-Object回圈之外,因此,您只讀取該變數賦值的最后一個結果。你可能想要:
$thisDataArray | Select-String -AllMatches -Pattern $regex -CaseSensitive | ForEach-Object {
$letters = $_.Matches.Value
Write-Host $letters
}
不過,值得注意的是,除非捕獲或重定向,否則到控制臺的輸出是隱式的,因此Write-Host可能不需要也不需要變數賦值:
$thisDataArray | Select-String -AllMatches -Pattern $regex -CaseSensitive | ForEach-Object {
$_.Matches.Value
}
- 為什么我只能從我的 ? 中得到一個陣列成員“麻煩”
$regex?
你沒有,你會得到cadabra并trouble假設
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515925.html
標籤:数组电源外壳目的变量范围
