PowerShell: Combine two hash tables

送分小仙女□ 提交于 2019-11-28 13:56:48
JPBlanc

Given :

f1.csv is :

SkuID,SkuStatusCode,QuantityOnHand
1828,True,441 
3022,True,325 
2981,True,214 
2989,True,842 

f2.csv is :

SkuID,RegularPrice,CurrentPrice
1828,49.99,48.99
3022,25,19.99
2981,45,39.99
2989,28,18.99

Try this far-fetched solution, not so good as join-object because you need to know the properties. You also have to be careful here with + operator between $a and $b which is not commutative it changes the group order :

   $a = Import-Csv C:\temp\f1.csv
   $b  = Import-Csv C:\temp\f2.csv
   $b + $a | Group-Object -Property skuId  | 
% {$x= New-Object -TypeName psCustomObject -Property 
@{SkuID=$_.name;RegularPrice=$_.group[0].RegularPrice;
CurrentPrice=$_.group[0].CurrentPrice;
SkuStatusCode=$_.group[1].SkuStatusCode;QuantityOnHand=$_.group[1].QuantityOnHand};
 $x}

for me it gives :

QuantityOnHand : 441 
RegularPrice   : 49.99
SkuStatusCode  : True
SkuID          : 1828
CurrentPrice   : 48.99

QuantityOnHand : 325 
RegularPrice   : 25
SkuStatusCode  : True
SkuID          : 3022
CurrentPrice   : 19.99

QuantityOnHand : 214 
RegularPrice   : 45
SkuStatusCode  : True
SkuID          : 2981
CurrentPrice   : 39.99

QuantityOnHand : 842 
RegularPrice   : 28
SkuStatusCode  : True
SkuID          : 2989
CurrentPrice   : 18.99
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!