问题描述
我有两个哈希表,这些哈希表是根据来自两个不同XML文件的数据创建的.我想做的是根据两个表中的公共值将两个表组合成一个哈希表.
I have two hash tables created from data from two different XML files. What I would like to do is combine the two tables into a single hash table based on a common value in the both tables.
Inv Hash:
$invHash = $invXML.InventoryDto.ProductInventoryItem.SkuInventoryItem |
select @{ L = 'SkuID'; E = { $_.SkuId } }, @{ L = 'SkuStatusCode';
E = { if ($_.SkuStatusCode -eq 'Active') { 'True' } else { 'False'} } },
@{ L = 'QuantityOnHand'; E = { $_.QuantityOnHand } }
$ invHash的示例内容:
Sample Contents of $invHash:
SkuID SkuStatusCode QuantityOnHand
----- ------------- --------------
1828 True 441
3022 True 325
2981 True 214
2989 True 842
PriceHash:
PriceHash:
$priceHash = $priceXML.PricingDto.ProductPricingItem.SkuPricingItem |
select @{ L = 'SkuID'; E = { $_.SkuId } }, @{ L = 'RegularPrice';
E = { $_.PriceGroup.RegularPrice } }, @{ L = 'CurrentPrice';
E = { $_.PriceGroup.CurrentPrice } }
$ priceHash的示例内容:
Sample contents of $priceHash:
SkuID RegularPrice CurrentPrice
----- ------------- --------------
1828 49.99 48.99
3022 25 19.99
2981 45 39.99
2989 28 18.99
$ invpriceHash的所需内容:
Desired contents of $invpriceHash:
SkuID SkuStatusCode QuantityOnHand RegularPrice CurrentPrice
----- ------------- -------------- -------------- --------------
1828 True 441 49.99 48.99
3022 True 325 25 19.99
2981 True 214 45 39.99
2989 True 842 28 18.99
推荐答案
给出:
f1.csv是:
SkuID,SkuStatusCode,QuantityOnHand
1828,True,441
3022,True,325
2981,True,214
2989,True,842
f2.csv是:
SkuID,RegularPrice,CurrentPrice
1828,49.99,48.99
3022,25,19.99
2981,45,39.99
2989,28,18.99
尝试使用此远距离解决方案,不如join-object
好,因为您需要了解属性.您还必须注意,在$a
和$b
之间使用+
运算符,这不是可交换的,它会更改组顺序:
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}
对我来说,它给出了:
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
这篇关于PowerShell:合并两个哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!