I trying to join two files on a common field.
File one:
upc,qty
840197083284,2
840197083291,3
840197083307,21
840197083314,0
File two:
product_sku,size,color,upc
121,MD,Black,840197083277
121,LG,Black,840197083284
121,XL,Black,840197083291
121,2XL,Black,840197083307
I'm trying to join on upc.
I've read a number of post....
combining 2 files with a comon field which seems to be close but I can't make it work.
Update:
I'm new to perl and to perlmonks... So, First thanks for all the help.
Here is the code that worked for me.
open(I1,"Inventory.csv") or die "file1: $!";
$_ = <I1>; # read column headings
while (<I1>) { # get data
chomp;
my ($upc,$qty) = split /,/;
$data{$upc} = $qty;
}
open(I2,"Matrix.csv") or die "file2: $!";
$_ = <I2>; # read column headings
while (<I2>) { # get data
chomp;
my ($sku,$siz,$clr,$upc) = split /,/;
$data{$upc} .= join ',', '', $sku, $siz, $clr;
}
open(my $fh_out, '>', 'new.csv')
or die("Can't open output file: $!\n");
for (sort keys %data) {
my @content = "$data{$_},$_";
if (grep(/.*,.*,.*,.*$/, @content)){@column = grep(/.*,.*,.*,.*$/,
+ @content)};
#@column = grep(/.*,.*,.*,.*$/, @content);
print $fh_out "@column\n";
}
Edit: g0n - Linkified PM link
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.