Compare Two text files ?!?!?! 
Author Message
 Compare Two text files ?!?!?!

I have text fileA containing X number of lines, fileB contains Y
number of lines. Each line has a single word in it.

I want to compare each line in file A with every line in fileB, and if
the result matches it writes that line into a new file, else discard.

I have so far made following nested loops but not working correct. IS
there any built in fuction that can ease the task ?

Thanks in advance,

===========

        ### print SAMEIDS "\n$webmailid" ;

                if ($webmailid == $rhinoid) {
                        print "\The ID $webmailid is in both places";
                        ### last;
                }
                else {
                        print "\nThe ID $webmailid not found in rhino."
                        ### print SAMEIDS "\n$webmailid" ;
                }
                ###### print SAMEIDS "\n$rhinoid" ;
        }

Quote:
}



Wed, 25 Aug 2004 08:00:48 GMT  
 Compare Two text files ?!?!?!


Quote:
> I want to compare each line in file A with every line in fileB, and if
> the result matches it writes that line into a new file, else discard.

> I have so far made following nested loops but not working correct.


> if ($webmailid == $rhinoid) {
> print "\The ID $webmailid is in both places";
> }
> else {
> print "\nThe ID $webmailid not found in rhino."
> }
> }
> }

You have 2 syntactic errors.  'if ($webmailid == $rhinoid)' should be 'if
($webmailid eq $rhinoid)' because you're doing comparison of strings, not
mathematical quantities. And you've left off the 'n' in the '\n' at the
start of the first print statement.  Use strict; use warnings!

Quote:
> ISthere any built in fuction that can ease the task ?

Not to my knowledge.  However, the Perl Cookbook has several recipes in
Chapter 4 that address this type of problem.


Wed, 25 Aug 2004 14:39:15 GMT  
 Compare Two text files ?!?!?!

Quote:



> > I want to compare each line in file A with every line in fileB, and if
> > the result matches it writes that line into a new file, else discard.

> > I have so far made following nested loops but not working correct.



> > if ($webmailid == $rhinoid) {
> > print "\The ID $webmailid is in both places";
> > }
> > else {
> > print "\nThe ID $webmailid not found in rhino."
> > }
> > }
> > }

> You have 2 syntactic errors.  'if ($webmailid == $rhinoid)' should be 'if
> ($webmailid eq $rhinoid)' because you're doing comparison of strings, not
> mathematical quantities. And you've left off the 'n' in the '\n' at the
> start of the first print statement.  Use strict; use warnings!

These are errors, but not syntax errors.  Yes, warnings would have caught
them.

Quote:
> > ISthere any built in fuction that can ease the task ?

> Not to my knowledge.  However, the Perl Cookbook has several recipes in
> Chapter 4 that address this type of problem.

Well, there is grep, which can replace the inner loop.

Otherwise, what the OP wants is basically the intersection of two
arrays.  There is a FAQ that deals with that, find it using
"perldoc -q intersection".

Anno



Fri, 27 Aug 2004 12:50:32 GMT  
 Compare Two text files ?!?!?!

Quote:
> I have text fileA containing X number of lines, fileB contains Y
> number of lines. Each line has a single word in it.

> I want to compare each line in file A with every line in fileB, and if
> the result matches it writes that line into a new file, else discard.

You are too deeply engaged in implementation details already.
What you are really looking for seems to be the intersection of A and B.

Please see "perldoc -q intersection" for an easy and proven algorithm.

jue



Fri, 27 Aug 2004 21:24:30 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. To compare two files and search for a word in both files

2. Comparison of two fields in two text files

3. perl module to compare two files?

4. compare two files

5. compare two files - help!!

6. Help comparing two files using hashes

7. comparing the time stamp of two files

8. How to compare two files?

9. To compare the content in two files..

10. compare two files

11. File I/O: want to compare two or more lines @ once

12. Compare two Paths in a file impossible

 

 
Powered by phpBB® Forum Software