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

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;

Replies are listed 'Best First'.
Re^5: comparing contents of the file
by toolic (Bishop) on Jun 04, 2008 at 16:38 UTC
    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;
        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)