open(I1,"Inventory.csv") or die "file1: $!"; $_ = ; # read column headings while () { # get data chomp; my ($upc,$qty) = split /,/; $data{$upc} = $qty; } open(I2,"Matrix.csv") or die "file2: $!"; $_ = ; # read column headings while () { # 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"; }