加入两个哈希表来制作一个 [英] Join two hashtables to make one

查看:19
本文介绍了加入两个哈希表来制作一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个哈希表,我需要比较它们.让我解释一下我的问题:

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

实际上,我只有一个值(我没有成功拥有多个值.例如 Jack 必须有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆