Date: Wed, 03 Oct 2001 19:38:33 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: append column upon match (newbie)
Message-Id: <mbudash-D085EB.12383503102001@news.sonic.net>

In article <30688683.0110031022.3baabefb@posting.google.com>, 
yus_punju@hotmail.com (Yugal Sharma) wrote:

> Hi,
> 
> I've been struggling with this one for a while.  I'd really appreciate
> any thoughts I could get on it.
> 
> Assuming I have a data_file that looks like this (space delimited):
> 
> 1AKK	LYS	27	88	CA	3.7847	  NZ	10.1088
> 1AKK	LYS	72	73	CA	3.8272	  NZ	13.8547
> 1AKK	LYS	99	100	CA	3.8160	  NZ	10.6881
> .
> .
> 
> And I have an input_file that looks like this (space delimited):
> 
> 72  73   16.4(BS3)
> 27  34   6.4(erg)
> 99  100  4.3(gsh)
> 
> What I would like to do is compare the 1st and 2nd fields of the
> input_file to the 3rd and 4th fields of the data_file on a
> line-by-line basis.  If there is a match (on both fields), append the
> third field of the input_file as a last field to the data_file.  If
> there is no match, do not append anything.
> 
> So, for example, comparing the input_file to the data_file above would
> yield the final_file:
> 
> 1AKK	LYS	27	88	CA	3.7847	  NZ	10.1088
> 1AKK	LYS	72	73	CA	3.8272	  NZ	13.8547  16.4(BS3)
> 1AKK	LYS	99	100	CA	3.8160	  NZ	10.6881  4.3(gsh)
> .
> .
> 

assuming enough ram is available to load both files into memory, here's 
one way:

#-------------------------------
use strict;

my $data = 'data_file';
open (DATAFILE, $data) or die ("Can't open $data for reading: $!");
my @data = <DATAFILE>;
close DATAFILE;

my $input = 'input_file';
open (INPUTFILE, $input) or die ("Can't open $input for reading: $!");
my @input = <INPUTFILE>;
close INPUTFILE;
chomp @input;

foreach my $inputrcd (@input) {
  my @inputflds = split(/\s+/, $inputrcd);
  for my $i (0..$#data) {
    $datarcd = $data[$i];
    @dataflds = split(/\s+/, $datarcd);
    if ( ($inputflds[0] eq $dataflds[2]) &&
         ($inputflds[1] eq $dataflds[3]) ) {
      chomp $datarcd;
      $data[$i] = $datarcd . " " . $inputflds[2] . "\n";
    }
  }
}

open (DATAFILE, ">$data") or die ("Can't open $data for writing: $!");
print DATAFILE @data;
close DATAFILE;
#-