Join two hashtables to make one

瘦欲@ 提交于 2019-12-19 08:00:37

问题


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";
}

I want to have this kind of result :

Key         Value
---         -----
Jack        P1, P3
John        P5
Sarah       P2
Arnold      P3
Maurice       P5, P4

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).

How can I fix it?

I have already tried:

$User.GetEnumerator() | select Key, @{n='Value'; e={$Profil[$_.Value]}}

and

$User.GetEnumerator() | %{[PSCustomObject]@{aKey=$_.Key;bValue=$Profil[$_.Value]}}

Any idea?


回答1:


Use this expression

$User.GetEnumerator() | Select-Object Key, @{name='Value'; expression={($_.Value -split ", " | Foreach-Object {$Profil[$_]}) -join ", "}}

This basically creates an array of input values, get the values from $Profil for each element and then creates a string from these values.



来源:https://stackoverflow.com/questions/51539849/join-two-hashtables-to-make-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!