Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare Keys and overlay data

Status
Not open for further replies.

bigbalbossa

Programmer
Mar 21, 2002
87
US
I'm doing a string compare on two variables in two different files.

File1: contains a lookup key

File2: contains the lookup key, key2, and key3 field

If the file1 key matches a key in file2, i need to append the key2 and key3 fields to file1.

My current script is working properly, but runtime is way too long due to the way i'm performing my match.

First reading file2 into and array:
Code:
open(SAL, "$SAL_INI") || die "Can't open file: $SAL_INI\n";
while(<SAL>)
{
   chomp;
   my $data = $_;

   ( $key1[$i], $key2[$i], $key3[$i] ) = split( '\|', $data );

   $i++;
}

Then looping through my datafile:

Code:
    for ($n=0;$n<$i;$n++)
    {
      if ($KEY eq $key1[$n])
        {
          $search_idx = $n;
        }
    }

if ($search_idx != -1)
    {
    	#print "SAL match\n";
      $KEY2 = "$key2[$search_idx]";
      $KEY3 = "$key3[$search_idx]";
    }
    else
    {
     ...
    }

Any thoughts on other ways to accomplish this???
 
This is screaming "hash" to me.

Can you give some "before and after" datafiles to show exactly what you are after?
 
Thanks for your response brigmar...i'm working on a hash solution now, actually a hash of hash of hash :) But i'd like to see what you come up with. Here are the data files (pipe del):

File1 (lookup key):

RABBI
MR RABBI
RADM
REV
SONR

File 2 (lookup key + key2 + key3)
RABBI|RB|1
MRABI|RB|2
RADM|RAD|2
REV|RV|7

Output:

RABBI|RB|1
MR RABBI||
RADM|RAD|2
REV|RV|7
SONR||

 
Top of my head (I haven't included the file open/close commands, etc.):

Code:
my %hash=();
while(<FILE2>) {
  chomp;
  $hash{$1} = $_ if ( /(.*?)\|/ );
}

while(<FILE1>) {
  chomp;
  if(exists($hash{$_})) {
    print OUTFILE $hash{$_}."\n";
  } else {
    print OUTFILE $_."||\n";
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top