Nagarajan Lakshmanan
Nagarajan Lakshmanan

Reputation: 21

Merge Two hashtables and get the common values by grouping in Powershell

I need to merge two hashtables in into a single PSCustomObject or another hashtable (doesn't matter) and wanted to group the output as below.

1) Firstly I wanted to look for keys from Hashtable1 in hashtable2 to get the values.

2) Group the values within hashtable1 and display count of unique keys. I am able to get this done using the below cmdlets, but I didn't to succeed with other two objectives.

$uniquevalues = @{}
$hastable1.Values | Sort | % { $uniquevalues["$_"] += 1 }

3) Sum up the values from hashtable2 grouped by key.

$hastable1
E737A146 -4                                                                                                                                  
F637A146 14                                                                                                                                  
E637A146 10                                                                                                                                  
E537A146 -4                                                                                                                                  

$hastable2
E737A146 1                                                                                                                                   
F637A146 7                                                                                                                                   
E637A146 2                                                                                                                                   
E537A146 1 

Expected Output:
unique_Values_From_HashTable1 || Count_unique_keys_HashTable1 || Sum_Values_Hashtable2_ByKey&ByValuesFrom Hashatable1
-4 2 2
14 1 7
10 1 2

Upvotes: 2

Views: 646

Answers (1)

leeharvey1
leeharvey1

Reputation: 1436

Welcome to SO. Here's one possible solution, although I suspect there may be a more efficient technique that can avoid the nested loops:

$hashtable1 = @{
    E737A146=-4
    F637A146=14
    E637A146=10
    E537A146=-4
}

$hashtable2=@{
    E737A146=1
    F637A146=7
    E637A146=2
    E537A146=1
}

# 2) Group the values within hashtable1 and display count of unique keys.
$uniquevalues = @{}
$hastable1.Values | Sort | % { $uniquevalues["$_"] += 1 }

# 3) Sum up the values from hashtable2 grouped by key.
$results = @{}
foreach ($uVal in $uniquevalues.Keys) {
    foreach ($item1 in $hashtable1.Keys) {
        if ($uVal -eq "$($hashtable1[$item1])") {
            foreach ($item2 in $hashtable2.Keys) {
                if ($item1 -eq $item2) {
                    $results[$uVal] = [ordered]@{
                        unique_Values_From_HashTable1 = $uVal
                        Count_unique_keys_HashTable1 = $uniquevalues[$uVal]
                        Sum_Values_Hashtable2_ByKey = $results[$uVal].Sum_Values_Hashtable2_ByKey + $hashtable2[$item2]
                    }
                }
            }
        }
    }
}

($results.Values | Select-Object -Property Keys -Unique).Keys -join ' || '
foreach ($k in $results.Keys) {
    "{0} {1} {2}" -f 
        $results[$k].unique_Values_From_HashTable1, 
        $results[$k].Count_unique_keys_HashTable1, 
        $results[$k].Sum_Values_Hashtable2_ByKey
}

Output:

unique_Values_From_HashTable1 || Count_unique_keys_HashTable1 || Sum_Values_Hashtable2_ByKey
-4 2 2
10 1 2
14 1 7

Hope this helps.

Upvotes: 1

Related Questions