英文:
Is there any way to convert case-sensitive hashtable into case-insensitive hashtable?
问题
我有一个区分大小写的哈希表,我想将它转换为不区分大小写的。
我知道 PowerShell 基本上是不区分大小写的。然而,在 Azure PowerShell 命令中并非如此。
例如,假设我有一个名为 "exampleRG" 的 Azure 资源组,其中有一个标签 "owner:jdoe@example.com"。
$A
是一个区分大小写的哈希表。
$A = (Get-AzResourceGroup -name "exampleRG").Tags
$A.Owner # 返回空值
$A.owner # 返回 jdoe@example.com
我想将其设置为不区分大小写,以便可以通过不区分大小写的键获取值。
(即 $A.Owner # 返回 jdoe@example.com
, $A.OWNER # 返回 jdoe@example.com
)
我尝试过筛选键,如下所示
$A = (Get-AzResourceGroup -name "exampleRG").Tags | where-object {$_.keys -like "owner";}
但什么都没有改变。
有没有办法做到这一点?
英文:
I have a case-sensitive hashtable, and I want to convert it into case-insensitive.
I know PowerShell is basically case-insensitive in nature.
However, that is not the case in Azure PowerShell command.
For example, let's say I have a Azure Resource Group "exmapleRG" that has a tag "owner:jdoe@example.com".
$A
is a case-sensitive hashtable.
$A = (Get-AzResourceGroup -name "exampleRG").Tags
$A.Owner # returns nothing
$A.owner # returns jdoe@example.com
I want to make it case-insensitive so that I can get values by case-insensitive keys.
(i.e. $A.Owner # returns jdoe@example.com
, $A.OWNER # returns jdoe@example.com
)
I tried to filtering keys like below
$A = (Get-AzResourceGroup -name "exampleRG").Tags | where-obejct {$_.keys -like "owner"}
but nothing changed.
Is there any way to do this?
答案1
得分: 1
不可能将区分大小写的哈希表转换为不区分大小写的哈希表,解决方法是重新创建它:
$hash = [hashtable]::new()
$hash['owner'] = 'jdoe@example.com'
$hash.Owner # null
$caseInsensitiveHash = [hashtable]::new($hash, [StringComparer]::OrdinalIgnoreCase)
$caseInsensitiveHash.Owner # jdoe@example.com
Hashtable(IDictionary, IEqualityComparer)
构造函数重载 会复制传入的现有哈希表并使用指定的比较器。
英文:
It's not possible to convert a case-sensitive hash table into a case-insensitive one, the way around it is to recreate it:
$hash = [hashtable]::new()
$hash['owner'] = 'jdoe@example.com'
$hash.Owner # null
$caseInsensitiveHash = [hashtable]::new($hash, [StringComparer]::OrdinalIgnoreCase)
$caseInsensitiveHash.Owner # jdoe@example.com
The Hashtable(IDictionary, IEqualityComparer)
overload will make a copy of the existing hash table passed as argument and use the specified comparer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论