Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Joining two files on common field

by Skeeve (Parson)
on Sep 23, 2005 at 06:10 UTC ( [id://494410]=note: print w/replies, xml ) Need Help??


in reply to Re: Joining two files on common field
in thread Joining two files on common field

You said: That's pretty brittle in a number of ways: ... it depends rigidly on a particular ordering and quantity of columns in each file

So let's fix that.

$_ = <I1>;   # read column headings
becomes
$_ = <I1>; # read column headings my @columns= split /,/;


And when reading the columns, instead of
my ($upc,$qty) = split /,/;
and
my ($sku,$siz,$clr,$upc) = split /,/;
we put
@line{@columns}= split /,/;
(provided you have a my %line; defined)

So in full:
use strict; my %data; open(I1,"file1") or die "file1: $!" $_ = <I1>; # read column headings my @columns= split /,/; my %line; while (<I1>) { # get data chomp; @line{@column} = split /,/; $data{$line{'upc'}} = $line{'qty'}; } open(I2,"file2") or die "file2: $!" $_ = <I2>; # read column headings my @columns= split /,/; while (<I2>) { # get data chomp; @line{@column} = split /,/; $data{$line{'upc'}} .= join ',',@line{@columns},''; # or: $data{$line{'upc'}} = join ',', $data{$line{'upc'}},@line{@colum +ns}; } for (sort keys %data) { print $data{$_}\n"; }

$\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://494410]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-28 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found