2

Hello i'm beginner and I need to compare two hashtable and to have an other respawn.

For example :

[hashtable]$alpha =@{
"A1" = "computer";
"A2" = "folder";
"A3" = "plane";
"A4" = "flower";
"A5" = "dog";
}


[hashtable]$beta =@{
"computer" = "P1";
"plane" = "P2";
"garden" = "p3";
"flower" = "P4";
"dog" = "P5";
}

if i have Computer in $alpha and in $beta i need to write P1 for the user A1 if i have plane in $alpha and in $beta i need to write P2 for the user A3

Do i need to use for each ?

Thanks !

10
  • 3
    Have you tried anything? Commented Jul 25, 2018 at 9:03
  • 1
    there is a compare-object function on the internet Commented Jul 25, 2018 at 9:13
  • I try compare-object by i didn't succeed to manipulate with $alpha.keys Commented Jul 25, 2018 at 9:22
  • 1
    What does your compare-object and "manipulate with $alpha.keys" code look like? What output did you get (if any) and how does that differ from what you were expecting? Commented Jul 25, 2018 at 9:31
  • if i have plane in $alpha and in $beta i need to write P2 for the user A2 Can you explain this? Why P2 for A2? plane in P2 and A3. Commented Jul 25, 2018 at 9:34

1 Answer 1

9

The solution has been already provided by @PetSerAl and @LotPings and is one of the following

$alpha.GetEnumerator() | select Key, @{ n='Value'; e={$beta[$_.Value]} }
$alpha.GetEnumerator() | %{[PSCustomObject]@{aKey=$_.Key;aValue=$_.Value;bValue=$beta[$_.Value]}}

Let me explain what exactly happens there.

First of all, as you use hashtables you cannot manipulate them directly using cmdlets like Select-Object. In order to do this you need to use GetEnumerator() method on it. Now you can pipe it to Select-Object.

To use the values from another hashtable you have to use calculated property instead of standard one. The syntax of it is:

@{ n='name'; e={ expression to be executed }

Let's dig into this expression $beta[$_.Value] a bit more. $_ represents the object sent to pipeline so $_.Value is its value (as you know hashtables have key names and values). To better understand check this expression and its result

PS C:\> $alpha.GetEnumerator() | select -Last 1

Name                           Value
----                           -----
A5                             dog

For this entry $_.Value is dog so $beta[$_.Value] is evaluated to $beta["dog"] and its value is:

PS C:\> $beta["dog"]
P5

Additional resources:

  1. Microcode: PowerShell Scripting Tricks: The Joy of using Hashtables with Windows PowerShell
  2. Add a calculated property with Select-Object in PowerShell
  3. Example of the same but with multiple values to be replaced (my answer)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.