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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.