简体   繁体   中英

Powershell: Array vs Hashtable (or both) not sure how to proceed

I have the following powershell script:

$grouped_TPR_Test1=Import-Csv c:\TPR.csv | group UPC -AsHashTable -AsString
Import-Csv c:\HQ.csv  | foreach{
$tpr_Sales=($grouped_TPR_Test1."$($_.UPC)" | foreach {$_.TPR_Sales}) -join ","
$_ | Add-Member -MemberType NoteProperty -Name TPR_SALES -Value $tpr_Sales -PassThru 
} | Export-Csv -NoTypeInformation c:\HQ_TPR_sales.csv

It finds/matches the UPC value in file c:\\TPR.csv with the same value in this file. c:\\HQ.csv and outputs the corresponding sales data from to a 3rd file that includes all fields in c:\\HQ.csv as well as the additional ones that match on UPC from c:\\TPR.csv

This works.

However, I am not sure how to add a second field to check ( "Zone" , to narrow down the results that are sent to the 3rd output file. Both files have the zone field as well.

I read a bit on this and an array seems better suited for multiple criteria, rather than a hashtable, but I'm not having much luck.

c:\HQ.csv looks essentially like this:                                    

UPC           ZONE column1  column2  column3
1234567890123   3    blah1  blah2    blah3

c:\TPR.csv looks essentially like this:

UPC            ZONE sales
1234567890123   3   5.00
1234567890123   2   4.00
3210987654321   2   3.00

Any help is appreciated.

Thanks!

You could simply just use WHERE-OBJECT on the resulting file to just pull out the zone(s) you are interested in:

Import-CSV c:\HQ_TPR_sales.csv | WHERE-OBJECT {$_.Zone -eq 2}

Or put the WHERE-OBJECT into your pipe before you export-csv:

... | ? {$_.Zone -eq 2} | Export-CSV ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM