连接两个哈希表以使一个 [英] Join two hashtables to make one
本文介绍了连接两个哈希表以使一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个哈希表,需要对其进行比较。让我解释一下我的问题:
I have two hash tables and I need to compare them. Let me explain my problem :
[hashtable]$User = @{
"Jack" = "AdminLA, AdminUSA";
"John" = "AdminAustralia";
"Sarah" = "AdminIceland";
"Arnold" = "AdminUSA";
"Maurice" = "AdminAustralia, AdminCanada";
}
[hashtable]$Profil = @{
"AdminLA" = "P1";
"AdminIceland" = "P2";
"AdminUSA" = "P3";
"AdminCanada" = "P4";
"AdminAustralia" = "P5" ;
"AdminCroatia" = "P6";
}
我想要这样的结果:
Key Value
--- -----
Jack P1, P3
John P5
Sarah P2
Arnold P3
Maurice P5, P4
实际上,我只有一个值(我没有成功拥有多个值。例如,杰克必须拥有P1和P3,而我只有P1)。
Actually, I have only one value (I haven't succeeded to have multiple values. For example Jack must have P1 and P3 and I have only P1).
如何解决?
我已经尝试过:
$User.GetEnumerator() | select Key, @{n='Value'; e={$Profil[$_.Value]}}
和
$User.GetEnumerator() | %{[PSCustomObject]@{aKey=$_.Key;bValue=$Profil[$_.Value]}}
有什么想法吗?
推荐答案
使用此表达式
$User.GetEnumerator() | Select-Object Key, @{name='Value'; expression={($_.Value -split ", " | Foreach-Object {$Profil[$_]}) -join ", "}}
这基本上会创建一个输入值数组,从 $ Profil
中获取每个元素的值,然后从这些元素中创建一个字符串值。
This basically creates an array of input values, get the values from $Profil
for each element and then creates a string from these values.
这篇关于连接两个哈希表以使一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文