Powershell 哈希 table 和重复键
Powershell hash table and duplicate keys
我正在使用哈希 table 以希腊字符存储一些名称和 ID。
$hsNames = @{}
$hsNameID = 1
$name = "Νικος"
$hsNames.Add($name, $hsNameID)
$hsNameID++
$name = "Νίκος"
$hsNames.Add($name, $hsNameID)
$hsNames
上面的输出是:
Name Value
---- -----
Νικος 1
Νίκος 2
这意味着当其中一个带有希腊口音时,为相同的名称创建了两个键。现在我不希望这种情况发生,我只需要一个具有第一个 ID (1) 的密钥 - MySQL 中 utf8_unicode_ci 的行为。我想我需要以某种方式告诉 powershell 在字符串比较中使用 Unicode 归类算法 (http://www.unicode.org/reports/tr10/tr10-33.html)。但是怎么办?
有趣的问题,尽管有人可能会争辩说这两个名字是不同的 因为 口音。您必须决定是存储原始拼写和 "normalized" 拼写,还是只存储标准化拼写,因为转换是一个 one-way 过程。
我找到了两个提供解决方案方法的链接。
Ignoring accented letters in string comparison and the PowerShell version of this same C# code.
使用 ISE 中的 PowerShell 脚本,我能够编写以下内容:
$hsNames = @{}
$hsNameID = 1
$name1 = "Νικος"
$hsNames.Add($name1, $hsNameID)
$hsNameID++
$name2 = "Νίκος"
$hsNames.Add($name2, $hsNameID)
$hsNames
$new1 = Remove-StringDiacritic $name1
$new2 = Remove-StringDiacritic $name2
"With Diacritic removed"
$new1
$new2
$new1 -eq $new2
输出为:
Name Value
---- -----
Νικος 1
Νίκος 2
With Diacritic removed
Νικος
Νικος
True
基于此,您可以 "normalize" 您的字符串,然后再插入您的散列 table,最终您将得到一个 Key,而不是您想要的两个。
我正在使用哈希 table 以希腊字符存储一些名称和 ID。
$hsNames = @{}
$hsNameID = 1
$name = "Νικος"
$hsNames.Add($name, $hsNameID)
$hsNameID++
$name = "Νίκος"
$hsNames.Add($name, $hsNameID)
$hsNames
上面的输出是:
Name Value ---- ----- Νικος 1 Νίκος 2
这意味着当其中一个带有希腊口音时,为相同的名称创建了两个键。现在我不希望这种情况发生,我只需要一个具有第一个 ID (1) 的密钥 - MySQL 中 utf8_unicode_ci 的行为。我想我需要以某种方式告诉 powershell 在字符串比较中使用 Unicode 归类算法 (http://www.unicode.org/reports/tr10/tr10-33.html)。但是怎么办?
有趣的问题,尽管有人可能会争辩说这两个名字是不同的 因为 口音。您必须决定是存储原始拼写和 "normalized" 拼写,还是只存储标准化拼写,因为转换是一个 one-way 过程。
我找到了两个提供解决方案方法的链接。 Ignoring accented letters in string comparison and the PowerShell version of this same C# code.
使用 ISE 中的 PowerShell 脚本,我能够编写以下内容:
$hsNames = @{}
$hsNameID = 1
$name1 = "Νικος"
$hsNames.Add($name1, $hsNameID)
$hsNameID++
$name2 = "Νίκος"
$hsNames.Add($name2, $hsNameID)
$hsNames
$new1 = Remove-StringDiacritic $name1
$new2 = Remove-StringDiacritic $name2
"With Diacritic removed"
$new1
$new2
$new1 -eq $new2
输出为:
Name Value
---- -----
Νικος 1
Νίκος 2
With Diacritic removed
Νικος
Νικος
True
基于此,您可以 "normalize" 您的字符串,然后再插入您的散列 table,最终您将得到一个 Key,而不是您想要的两个。