這里我有 2 個變數:$repo和$growth_repo_name_list. 這是每個的輸出:
> $repo
growth_medicare_consumertargetinganalyzer
> $growth_repo_name_list
2021.2
growth_ifp_consumersummary_v2
growth_ifp_consumertargetinganalyzer
growth_medicare_all_usconsumerattributes
growth_medicare_consumersummary_v2
growth_medicare_consumertargetinganalyzer
growth_medicare_growthopportunityindex
growth_medicare_ifp_all_usconsumerattributes
growth_medicare_ntmtargetinganalyzer
marketintel_medicare_historicalenrollmentanalyzer
marketintel_medicare_planbenefits
memberintel_ifp_aepoepreporting_v2
memberintel_ifp_memberreporting_v2
memberintel_medicaid_memberreporting_v2
memberintel_medicare_aepoepreporting_v2
memberintel_medicare_memberreporting_v2
$repo是一個字串,并且$growth_repo_name_list是一個字串陣列:
> $repo.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
> $growth_repo_name_list.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$repo 如以下代碼的輸出所示,沒有隱藏字符:
> [System.Text.Encoding]::UTF8.GetBytes($repo)
103
114
111
119
116
104
95
109
101
100
105
99
97
114
101
95
99
111
110
115
117
109
101
114
116
97
114
103
101
116
105
110
103
97
110
97
108
121
122
101
114
> [System.Text.Encoding]::UTF8.GetBytes($growth_repo_name_list[5])
103
114
111
119
116
104
95
109
101
100
105
99
97
114
101
95
99
111
110
115
117
109
101
114
116
97
114
103
101
116
105
110
103
97
110
97
108
121
122
101
114
即使我通過雙引號將 Array 轉換為扁平字串,它仍然回傳False:
> "$growth_repo_name_list"
2021.2 growth_ifp_consumersummary_v2 growth_ifp_consumertargetinganalyzer growth_medicare_all_usconsumerattributes growth_medicare_consumersummary_v2 growth_medicare_consumertargetinganalyzer growth_medicare_growthopportunityindex growth_medicare_ifp_all_usconsumerattributes growth_medicare_ntmtargetinganalyzer marketintel_medicare_historicalenrollmentanalyzer marketintel_medicare_planbenefits memberintel_ifp_aepoepreporting_v2 memberintel_ifp_memberreporting_v2 memberintel_medicaid_memberreporting_v2 memberintel_medicare_aepoepreporting_v2 memberintel_medicare_memberreporting_v2
> $ph = "$growth_repo_name_list"
> $repo -like $ph
False
> $ph.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
我在這里做錯了什么?比較的結果應該是True因為字串$repo包含在串列中$growth_repo_name_list。我在這里先向您的幫助表示感謝。
uj5u.com熱心網友回復:
所有的比較都會產生$True,你可能比較錯誤?
摘自about_Comparison_Operators:
當輸入是一個集合時,運算子回傳集合中與運算式右側值匹配的元素。如果集合中沒有匹配項,比較運算子回傳一個空陣列
$repo = 'growth_medicare_consumertargetinganalyzer'
$growth_repo_name_list = @(
'2021.2'
'growth_ifp_consumersummary_v2'
'growth_ifp_consumertargetinganalyzer'
'growth_medicare_all_usconsumerattributes'
'growth_medicare_consumersummary_v2'
'growth_medicare_consumertargetinganalyzer'
'growth_medicare_growthopportunityindex'
'growth_medicare_ifp_all_usconsumerattributes'
'growth_medicare_ntmtargetinganalyzer'
'marketintel_medicare_historicalenrollmentanalyzer'
'marketintel_medicare_planbenefits'
'memberintel_ifp_aepoepreporting_v2'
'memberintel_ifp_memberreporting_v2'
'memberintel_medicaid_memberreporting_v2'
'memberintel_medicare_aepoepreporting_v2'
'memberintel_medicare_memberreporting_v2'
)
[bool]($growth_repo_name_list -like $repo) # => True
$repo -in $growth_repo_name_list # => True
$growth_repo_name_list -contains $repo # => True
[bool]($growth_repo_name_list -match $repo) # => True
$growth_repo_name_list.Contains($repo) # => True
uj5u.com熱心網友回復:
tl;博士
# Is the value of $repo equal to at least one element of array $growth_repo_name_list?
$repo -in $growth_repo_name_list
筆記:
默認情況下,
-in執行的每個元素相等比較不區分大小寫,就像 PowerShell 通常一樣。但是,您可以為運算子名稱添加前綴
c以使其區分大小寫(-cin在上面的示例中。這適用于所有字串處理運算子,包括下面討論的運算子。
至于你嘗試了什么:
$repo -like $ph($ph正在"$growth_repo_name_list")
比較的結果應該是$true因為字串$repo在串列中$growth_repo_name_list
No: -like,通配符匹配運算子,既不執行(直接)子字串匹配,也不支持匹配RHS 字串陣列。
- 相反,它將 LHS - 單個輸入字串或輸入字串陣列 -與 RHS 上的單個 通配符運算式進行比較。
- 如果您不小心將陣列用作 RHS,則該陣列是字串化的,通過將(字串化的)陣列元素與它們之間的單個空格連接起來形成一個字串 - 這很少有幫助。
因此,直接的解決方法是反轉運算元:
# *Array* of string inputs.
# Return those array elements that match the wildcard expression stored in $repo
$growth_repo_name_list -like $repo
這將陣列的每個元素與$growth_repo_name_list中的通配符運算式進行比較$repo,并回傳匹配元素的子陣列。
在布爾背景關系(例如if ($growth_repo_name_list -like $repo) ...)中使用回傳陣列轉換為$false子陣列是否為空,$true否則。
但是,在您的情況下$repo只是一個字串文字(它不包含諸如*and 之類的通配符元字符?),因此沒有理由使用-like- 只需使用-eq:
# *Array* of string inputs.
# Return those array elements that are case-insensitively equal to the
# string stored in $repo.
$growth_repo_name_list -eq $repo
相反,如果您想將 的值$repo作為陣列元素的子字串進行匹配:
# *Array* of string inputs.
# Return those array elements that case-insensitively contain the
# string stored in $repo as a substring (or in full).
$growth_repo_name_list -lik "*$repo*"
退一步:似乎您只是想測驗 的值$repo是否包含在存盤在 中的陣列中$growth_repo_name_list,即該值是否完全等于(至少)陣列元素之一。
為此,PowerShell 提供了兩個功能等效的運算子-in和-contains,它們僅在運算元順序上有所不同:
# Is the value of $repo equal to at least one element of array $growth_repo_name_list?
$repo -in $growth_repo_name_list
# Ditto, operands reversed.
$growth_repo_name_list -contains $repo
這些運算子直接回傳一個布林值($true或$false)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/363477.html
