1 powershell hash duplicates having
想象一下以下哈希:
$h=@{}
$h.Add(1,'a')
$h.Add(2,'b')
$h.Add(3,'c')
$h.Add(4,'d')
$h.Add(5,'a')
$h.Add(6,'c')
Run Code Online (Sandbox Code Playgroud)
什么查询会返回2个重复值'a'和'c'?
基本上我正在寻找PowerShell等效的以下SQL查询(假设表h(c1,c2):
select c1
from h
group by c1
having count(*) > 1
Run Code Online (Sandbox Code Playgroud)
你可以试试这个:
$h.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }
Count Name Group
----- ---- -----
2 c {System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
2 a {System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
Run Code Online (Sandbox Code Playgroud)
如果存储结果,则可以深入到组中以获取重复条目的键名.防爆.
$a = $h.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }
#Check the first group(the one with 'c' as value)
$a[0].Group
Name Value
---- -----
6 c
3 c
Run Code Online (Sandbox Code Playgroud)