ch1 has asked for the wisdom of the Perl Monks concerning the following question:
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.
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Joining two files on common field
by jZed (Prior) on Sep 22, 2005 at 20:38 UTC | |
by ch1 (Novice) on Sep 22, 2005 at 22:54 UTC | |
by jZed (Prior) on Sep 22, 2005 at 23:00 UTC | |
by ch1 (Novice) on Sep 23, 2005 at 04:53 UTC | |
by jZed (Prior) on Sep 23, 2005 at 05:10 UTC | |
| |
by ikegami (Patriarch) on Sep 23, 2005 at 17:35 UTC | |
Re: Joining two files on common field
by ikegami (Patriarch) on Sep 22, 2005 at 20:40 UTC | |
Re: Joining two files on common field
by davidrw (Prior) on Sep 22, 2005 at 21:59 UTC | |
by ambrus (Abbot) on Apr 12, 2006 at 12:59 UTC | |
by ch1 (Novice) on Sep 22, 2005 at 23:01 UTC | |
by davidrw (Prior) on Sep 23, 2005 at 02:08 UTC | |
by ch1 (Novice) on Sep 23, 2005 at 04:57 UTC | |
by davidrw (Prior) on Sep 23, 2005 at 12:47 UTC | |
by ambrus (Abbot) on Sep 23, 2005 at 16:02 UTC | |
Re: Joining two files on common field
by graff (Chancellor) on Sep 22, 2005 at 22:57 UTC | |
by Skeeve (Parson) on Sep 23, 2005 at 06:10 UTC | |
Re: Joining two files on common field
by graff (Chancellor) on Sep 24, 2005 at 14:44 UTC | |
Re: Joining two files on common field
by samtregar (Abbot) on Sep 22, 2005 at 20:33 UTC |
Back to
Seekers of Perl Wisdom