in reply to comparing contents of the file

Have you learned about hashes yet? You are in need of an algorithm, and the one that you need will most likely involve the use of a hash. Something like this:
declare a hash (e.g. "my %lines." open the first input file while reading the first input one line at a time use the line as a hash key and assign "1" as the hash value close the file open output.file1 to hold "matching lines" open output.file2 to hold "distinct lines" open the second input file while reading the second input one line at a time if the current line exists as a hash key and the hash value is "1" print this line to the "matching lines" file increment the hash value otherwise print this line to the "distinct lines" file (maybe include the f +ile name) having read all input, now loop over all the keys of the hash if the hash value assigned to this key is still "1" print this hash key to the "distinct lines" file (maybe include t +he file name)
I think you'll find that the actual perl code for that will be somewhat shorter than what I've written, but (since I assume this is homework) you should write the code. There are lots of places to read about hashes in Perl. Have you checked the Tutorials here at the Monastery?

Replies are listed 'Best First'.
Re^2: comparing contents of the file
by Ms.Ranjan (Initiate) on Jun 04, 2008 at 15:07 UTC
    Thankyou...Hi i have written the code as per your algorithm
    #!/usr/bin/perl use strict; use warnings; my %lines; open F1, "File.txt"; while(my $result= <F1>) { $lines{$result}=1; } close(F1); open (OF1, ">match.txt"); open (OF2, ">diff.txt"); open F2,"File2.txt"; while(<F2>) { if ($lines{$_}==1) { print OF1 $lines; $lines++; } els { print OF2 $lines; } } close OF1; close OF2;
    and i am getting this error: Global symbol "$lines" requires explicit package name at line.pl line 21,22,26. i am gng throu the tutorials..
      You have declared a hash, my %lines;, but then you are trying to use that variable as if it were a scalar in this line:
      print OF1 $lines;

      You probably want:

      print OF1 $lines{$_};

      Same for OF2.

      Also, "els" is a typo: should be "else".

      $lines++; is also a problem.

        thanks..i have added the changes but getting error: Use of uninitialized value in numeric eq (==) at line.pl line 20, <F2> line 2. Use of uninitialized value in print at line.pl line 27, <F2> line 2.
        #!/usr/bin/perl use strict; use warnings; my %lines; open F1, "File.txt"; while(my $result= <F1>) { $lines{$result}=1; } close(F1); open (OF1, ">match.txt"); open (OF2, ">diff.txt"); open F2,"File2.txt"; while(<F2>) { if ($lines{$_} == 1) { print OF1 $lines{$_}; $lines{$_}++; } else { print OF2 $lines{$_}; } } close OF1; close OF2;