in reply to Comparing fields in 1 database with fields in another

If I were approaching this problem from a standing start, I would write out all the things I wanted to do in English, but break it up a bit so it reads clearly. So I might get something like:
for each line in File1, look at each line in File2 and for each of the fields in the current line if the field has the same stuff in it as the corresponding fie +ld in the current line of File 2 - remember it, otherwise - fuggedaboudit print out a report of all the stuff I remembered
In practice this might be a mental rather than a manual operation. The advantage of writing it down, with all those suggestive indentations, wd be that one could then take each line and slowly transmogrify it into executable perl. To start with you might rewrite the whole lot, but with the looping operators and accompanying brackets etc written in:
foreach (line in File1) { look at each line in File2; foreach (field in the current line) { if (the field has the same stuff in it as the corresponding fi +eld in the current line of File 2) { remember it; } else { fuggedaboudit; } } } print out a report of all the stuff I remembered;
This obviously still won't execute, but it's a framework that you can then colour in, as it were, by translating each of the non-perl bits into perl. So the first line might become
my @files = ('File1','File2'); my $file; foreach $file (@files) { do stuff with $file ...
In fact you might boil that line down to
foreach $file (File1..File2) { do stuff with $file ...
(which, because perl is wonderful, wd also work for more than two files). In fact you might boil it down further to
for (File1..File2) { do stuff with $_ ...
Then you would go on to the next line of your original outline and re-write that too. And when you get it all translated into perl you should put a -w after the shebang line at the top, and on the next line write use strict; Then when you run it, either it works and you go on your way rejoicing, or it fails but it tells you why.

If that's any help at all, I suggest you write some stuff, fix it up as much as you can and then, if it works, post it in triumph, and if it doesn't work, post it to see if anyone can tell you why.

§ George Sherston

Replies are listed 'Best First'.
Re: Re: Comparing fields in 1 database with fields in another
by rline (Initiate) on Oct 17, 2001 at 18:08 UTC
    To everyone who replied to my post, thankyou. Particularly to George, many thanks, as it was your thoughts that enabled me to solve this one. I'm pretty chuffed with the code I did, even though for most of you this will be very basic and easy perl. Nevertheless, in case anyone can use it, here it is. Of course, if anyone wants to offer improvements on it, please feel free...
    #!/usr/bin/perl print "Content-type: text/html\n\n"; $file1 = "file1.txt"; $file2 = "file2.txt"; open(FILE1,"<$file1"); @file1 = <FILE1>; close(FILE1); open(FILE2,"<$file2"); @file2 = <FILE2>; close(FILE2); $matches = 0; foreach $line ( @file1 ) { foreach $line2 ( @file2 ) { @array1 = split (/\|/, $line); @array2 = split (/\|/, $line2); $numavail = @array1; $limit = $numavail - 3; for ( $i=12; $i < $limit; $i++ ) { if ( $array1[$i] eq $array2[$i] ) { $matches++; } } if ( $matches >= 1 && $array1[14] ge $array2[14] && $array1[12 +] le $array2[12]) { print "Here is where I print the data I want to retreive, using things + like $array1[12] etc"; } $matches = 0; } }