In Union and Intersection in PowerShell? cool one-liners for set operations of arrays are described.
I want to do this with hashtables and have a solution using the keysets of the dictionaries. To extend then to the values, I use for-loops to iterate over the intersection of the keys and copy the values over to new result-hashtables. This looks not clean.
Further research showed solutions with GetEnumerator which is also not clean IMHO.
How can I replace the bloated for loops or enumerators by concise and nice one-liners?
Source code below:
http://paste.ubuntu.com/13362425/
# import csv
$a = Import-Csv -Path A.csv -Delimiter ";" -Header "Keys","Values"
$b = Import-Csv -Path B.csv -Delimiter ";" -Header "Keys","Values"
# Make nice hashtables for further use
$AData = @{}
foreach($r in $a)
{ $AData[$r.Keys] = $r.Values }
$BData = @{}
foreach($r in $b)
{ $BData[$r.Keys] = $r.Values }
# Set difference to find missing entries
$MissingA = $AData.Keys | ?{-not ($BData.Keys -contains $_)}
# I don't know how to do set-operations on hashtables yet. So use keysets and copy data (lame!)
$MissingAData = @{}
foreach($k in $MissingA)
{
$MissingAData[$k] = $AData[$k]
}
# Intersection
$Common = $AData.Keys | ?{$BData.Keys -contains $_}