当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 检查 HashTable 是否包含 PowerShell 中的键 [2 种方法]

作者:精品下载站 日期:2024-12-14 05:26:22 浏览:15 分类:玩电脑

检查 HashTable 是否包含 PowerShell 中的键 [2 种方法]


[玩转系统] 检查 HashTable 是否包含 PowerShell 中的键 [2 种方法]

使用ContainsKey()方法

要检查 HashTable 是否包含 PowerShell 中的键,请使用 if 语句和 .ContainsKey() 方法。

使用 ContainsKey() 方法:

$hashTable = @{'Name'='PowerShell'; 'Version'=7.0}
$key = 'Name'
if($hashTable.ContainsKey($key)){
    Write-Host "Key '$key' exists in the hash table."
}else{
    Write-Host "Key '$key' does not exist in the hash table."
}

输出 :

Key 'Name' exists in the hash table.

我们假设您对在 PowerShell 中创建哈希表有足够的了解,但让我们简要解释一下,以使每个人都能熟悉本文。在 PowerShell 中创建哈希表的最简单方法是使用 @{} 语法,如下所示:

创建哈希表:

$YourHashTableName = @{
'key1' = 'Value'
'key2' = 'Value'
'key3' = 'Value'
'key4' = 'Value'
}

我们可以使用换行符或分号 (;) 将每个键值对彼此分开。您可以在此处找到有关哈希表的更多详细信息。在上面的代码中,我们创建了一个名为 $hashTable 的哈希表,其中包含两个键值对:'Name'='PowerShell''Version'=7.0

然后,我们创建并初始化包含键(在本例中为 'Name')的 $key 变量,并使用 .ContainsKey() 方法来评估存储在 $key 变量中的密钥是否存在于 $hashTable 中。如果在哈希表中找到该键,则 .ContainsKey() 方法返回 True;否则,False

我们将 $hashTable.ContainsKey($key) 包含在 if 语句中,以使用 Write-Host cmdlet 显示自定义消息。如果 if 语句为 True/False,我们将分别通过以下方式通知用户您给定的密钥存在/不存在在控制台上显示一条消息。

这种方法允许我们通过简单地更改 $key 变量的值来快速检查哈希表中是否存在不同的键,这使得它更具可重用性。但是,必须注意 .ContainsKey() 方法默认情况下不区分大小写。

使用 ContainsKey() 方法:

$hashTable = @{'Name'='PowerShell'; 'Version'=7.0}
$keys = ('name', 'Name', 'Version', 'Names')
for ($i = 0; $i -lt $keys.Length; $i++){
    if($hashTable.ContainsKey($keys[$i])){
        Write-Host "Key '$($keys[$i])' exists in the hash table."
    }else{
        Write-Host "Key '$($keys[$i])' does not exist in the hash table."
    }
}

输出 :

Key 'name' exists in the hash table.
Key 'Name' exists in the hash table.
Key 'Version' exists in the hash table.
Key 'Names' does not exist in the hash table.

我们有一个 $keys 数组,其中包含上述代码的四个键。我们使用 for 循环来迭代这个数组;在每次迭代中,我们使用 .ContainsKey() 方法来检查每个键。最后,if-else 用于根据哈希表中是否找到指定的键来显示不同的消息。

我们知道$hashTable是如何创建的以及为什么我们使用if-else块。从现在开始,我们不会通过解释所有内容来详细说明,而是重点关注该方法中使用的方法/运算符。

使用 -contains 运算符

要检查 HashTable 是否包含 PowerShell 中的键,请使用带有 -contains 运算符的 if 语句。

使用 -contains 运算符:

$hashTable = @{'Name'='PowerShell'; 'Version'=7.0}
$key = 'Name'
if($hashTable.Keys -contains $key){
    Write-Host "Key '$key' exists in the hash table."
}else{
    Write-Host "Key '$key' does not exist in the hash table."
}

输出 :

Key 'Name' exists in the hash table.

或者,我们可以使用 .Contains() 方法来执行相同的操作。

使用 .Contains() 方法:

$hashTable = @{'Name'='PowerShell'; 'Version'=7.0}
$key = 'Name'
if($hashTable.Keys.Contains($key)){
    Write-Host "Key '$key' exists in the hash table."
}else{
    Write-Host "Key '$key' does not exist in the hash table."
}

输出 :

Key 'Name' exists in the hash table.

在上面的两个代码示例中,我们使用了 -contains 运算符和 .Contains() 方法;两者都会检查 $key 中存储的密钥是否存在于 $hashTable 中。

-contains 运算符是一个比较运算符,它返回一个布尔值,指示指定的值是否存在于 $hashTable 的键集合中。它将集合中的每个键与变量 $key 中存储的值进行一一比较,如果找到匹配则返回 True;否则,它将是False

一些学习者使用 -eq 运算符而不是 -contains,尽管 -eq 运算符有时可能会起作用,但使用 -eq 运算符来检查哈希表中的键集合是否等于字符串 'Name' 没有意义,因为字符串集合不能等于单个字符串。

更合适的做法是使用 $hashTable.Keys 集合上的 .Contains() 方法或 -contains 运算符来检查哈希表中的特定键。

需要注意的是,-contains 运算符不区分大小写,这意味着 'Name'name' 键是相同的。此外,在性能方面,-contains 运算符比 .Contains() 方法更快。

这就是如何在 PowerShell 中检查 HashTable 是否包含 key 的内容。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯