in reply to Re^2: comparing contents of the file
in thread comparing contents of the file

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.

Replies are listed 'Best First'.
Re^4: comparing contents of the file
by Ms.Ranjan (Initiate) on Jun 04, 2008 at 16:21 UTC
    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;
      It would be better to check for the existence of a hash key:
      if (exists $lines{$_})

      At this point, I would recommend GrandFather's approach.

        Hi the code is ok now..no error..thanks.. but a problem.. this actually compares the two files and displays the similar and non similar contents of only the File2.txt..but i want contents of both files to be displayed.. any suggestions please..
        #!/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 (defined $lines{$_}) { print OF1 $_; } else { print OF2 $_; } } close OF1; close OF2;