Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Joining two files on common field

by ikegami (Patriarch)
on Sep 22, 2005 at 20:40 UTC ( [id://494295]=note: print w/replies, xml ) Need Help??


in reply to Joining two files on common field

I believe you could use DBD::AnyData.

Or, here's something based on bart's solution in the mentioned thread:

use strict; use warnings; my %data; { open(my $fh_in, '<', 'file1.txt') or die("Can't open first input file: $!\n"); <$fh_in>; # Skip header. while (<$fh_in>) { chomp; my ($upc, @fields) = split /,/; push(@{$data{$key}}, @fields); } } { open(my $fh_in, '<', 'file2.txt') or die("Can't open second input file: $!\n"); <$fh_in>; # Skip header. while (<$fh_in>) { chomp; my @fields = split /,/; my $upc = pop(@fields); push(@{$data{$key}}, @fields); } } { open(my $fh_out, '>', 'file3.txt') or die("Can't open output file: $!\n"); foreach (sort keys %data) { print $fh_out (join(',', @$_), "\n"); } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (None)
    As of 2024-04-25 03:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found