How do I "merge" these 3 hash tables?

Hello,

I have been struggling with a simple “merge” of 3 simple hash tables.

$ht1 = @{1 = Bob ; 2 = Tom ; 3 = Jim}
$ht2 = @{1 = 100 ; 2 = 200 ; 3 = 300}
$ht3 = @{1 = 1000 ; 3 = 3000}

foreach ($mk in $ht1.GetEnumerator()) {
    $mk.Value + $delim + ($ht1.$mk).Value + $delim + ($ht2.$mk).Value |
    Out-file $outp -append
}

All 3 tables share the same key values/formats. The idea is to create a CSV file with 3 fields:

field-1 - value from $ht1, field-2 - value from $ht2, field-3 - value from $ht3.

I am getting correct values from $ht1 but empty from $ht2 and $ht3.

Would be grateful for any advice or suggestions. Sorry, I have found this new editor intimidating and I’ve only enclosed my PS code using what I’ve always known from the past: with “pre” and “slash-pre” tags enclosed in <> brackets. My apologies in advance. Would also appreciate knowing where I can learn this new editor’s features for posting code and explanations properly and neatly.

 

 

 

# Input
$htList = @(
    @{ 1 = 'Bob' ; 2 = 'Tom' ; 3 = 'Jim' }
    @{ 1 = 100   ; 2 = 200 ; 3 = 300 }
    @{ 1 = 1000  ; 3 = 3000 }
)

# Make one list of all key/value pairs
$PairList = foreach ($ht in $htList) {
    foreach ($kvPair in $ht.GetEnumerator()) { $kvPair }
}

# Group by (key) Name, spit out as PS Object 
$myOutput = foreach ($Group in ($PairList | Group Name | sort Name)) {
    $n=0
    $myObj = New-Object -TypeName PSObject 
    foreach ($Field in $Group.Group) {
        $n++
        $myObj | Add-Member -MemberType NoteProperty -Name "Field$n" -Value $Field.Value        
    }
    $myObj
}

# Output
$myOutput | FT -a 
$myOutput | Export-Csv .\mycsv1.csv -NoType
Field1 Field2 Field3
------ ------ ------
Bob       100   1000
Tom       200       
Jim       300   3000

Thank you Mr Boutros. I have studied your answer carefully. It opens up a lot of new PS knowledge to me, personally, in fundamentals and more advanced techniques that I’m still learning. Luckily (in desperation), I also found a blog that seems to have solved my problem. It used an alternative to GetEnumerator():

foreach ($mk in $ht1.Keys) {

$ht1.Item($mk) + $delim + $ht2.Item($mk) + $delim + $ht3.Item($mk) }

seems to work. My sincerest gratitude for your insightful solution. Best.