in reply to combining 2 files with a comon field
#! perl -w my %data; open IN, "file1.txt" or die "Ugh! $!"; while(<IN>) { chomp; my($key, $value) = split /\|/ or next; $data{$key}[0] = $value; } open IN, "file2.txt" or die "Ugh! $!"; while(<IN>) { chomp; my($key, $value) = split /\|/ or next; $data{$key}[1] = $value; } { open OUT, ">file3.txt" or die "Ugh! $!"; local($\, $,) = ("|\n", "|"); local $^W; # avoid "use of uninitialized value" foreach (sort keys %data) { print OUT $_, @{$data{$_}}[0, 1]; } }
The "or next" is to skip any empty lines in the input files. Disabling warnings in the printout is done to ignore warnings on any partly incomplete records.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: combining 2 files with a comon field
by jjohhn (Scribe) on May 18, 2005 at 11:47 UTC | |
by bart (Canon) on May 18, 2005 at 11:56 UTC | |
by TheStudent (Scribe) on May 18, 2005 at 12:03 UTC |