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

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;

Replies are listed 'Best First'.
Re^7: comparing contents of the file
by graff (Chancellor) on Jun 04, 2008 at 23:27 UTC
    You forgot a couple important parts of the algorithm:
    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 <-- YOU FORGOT THIS STEP YOU STILL NEED TO DO THIS PART: 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)
      thankyou very much for your help.