in reply to combining 2 files with a comon field
Hm, how about a hash per file and combine them on write?
untesteduse strict; use warnings; open FILE1, '<', 'file1.txt' or die ($!); open FILE2, '<', 'file2.txt' or die ($!); #- my %file1 = map { split '\|', $_ } <FILE1>; my %file1 = map { chomp && s/\|$//g && split '\|', $_, 2 } <FILE1>; #- my %file2 = map { split '\|', $_ } <FILE2>; my %file2 = map { chomp && s/\|$//g && split '\|', $_, 2 } <FILE2>; ## we now have A1=>'dog' in one hash, and A1=>'Fido' in the other close FILE1; close FILE2; open FILE3, '>', 'file3.txt' or die ($!); for (sort keys %file1) { print FILE3 join('|',$_,$file1{$_},$file2{$_}),'|',"\n"; } close FILE3;
Simply put, create a hash "map" of each file, then find where the keys intersect and print out the result.
Caveats:
Of course, that's not nearly as fun...my %file1; while (<FILE1>) { chomp; s/\|[\s]*$//; my ($key, $val) = split '\|', $_, 2; $file1{$key} = $val; }
The Eightfold Path: 'use warnings;', 'use strict;', 'use diagnostics;', perltidy, CGI or CGI::Simple, try the CPAN first, big modules and small scripts, test first.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: combining 2 files with a comon field
by jjohhn (Scribe) on May 18, 2005 at 16:18 UTC | |
by radiantmatrix (Parson) on May 18, 2005 at 20:42 UTC | |
by BerniBoy (Acolyte) on May 18, 2005 at 19:43 UTC |