如何检索散列 table 中的键,然后更新 Powershell 中的值?
How to retrieve a key in a hash table and then update the value in Powershell?
我有一个散列 table,其中键代表电子邮件地址,值代表计数。 if 检查查看电子邮件地址是否在散列 table 中,如果不包含,则将其添加到散列 table 并增加计数。
如果电子邮件地址存在于散列 table 中,我如何检索密钥然后更新值计数器?
谢谢!
$targeted_hash = @{}
$count = 0
foreach ($group in $targeted)
{
if (!$targeted_hash.ContainsKey('group.ManagerEmail'))
{
$targeted_hash.Add($group.ManagerEmail, $count + 1)
}
else
{
#TODO
}
}
PowerShell 提供了两个方便的快捷方式:
通过键分配给一个条目更新一个预先存在的条目,如果存在,或者为该键创建一个条目按需。
使用 ++
,新创建的条目上的增量运算符隐式地将值默认为 0
,因此将条目初始化为 1
因此:
$targeted_hash = @{}
foreach ($group in $targeted)
{
$targeted_hash[$group.ManagerEmail]++
}
循环后,散列 table 将包含所有不同经理电子邮件地址的条目,其中包含它们在输入数组 $group
.
中出现的次数
我有一个散列 table,其中键代表电子邮件地址,值代表计数。 if 检查查看电子邮件地址是否在散列 table 中,如果不包含,则将其添加到散列 table 并增加计数。
如果电子邮件地址存在于散列 table 中,我如何检索密钥然后更新值计数器?
谢谢!
$targeted_hash = @{}
$count = 0
foreach ($group in $targeted)
{
if (!$targeted_hash.ContainsKey('group.ManagerEmail'))
{
$targeted_hash.Add($group.ManagerEmail, $count + 1)
}
else
{
#TODO
}
}
PowerShell 提供了两个方便的快捷方式:
通过键分配给一个条目更新一个预先存在的条目,如果存在,或者为该键创建一个条目按需。
使用
++
,新创建的条目上的增量运算符隐式地将值默认为0
,因此将条目初始化为1
因此:
$targeted_hash = @{}
foreach ($group in $targeted)
{
$targeted_hash[$group.ManagerEmail]++
}
循环后,散列 table 将包含所有不同经理电子邮件地址的条目,其中包含它们在输入数组 $group
.