in reply to Re^3: Getting error when trying to compare two files
in thread Getting error when trying to compare two files

I have my expected output in one file which is of some 10 lines and when I execute perl program it will generate output which I am copying to a different file.

Now I need to compare actual vs expected? How I can achieve this?

  • Comment on Re^4: Getting error when trying to compare two files

Replies are listed 'Best First'.
Re^5: Getting error when trying to compare two files
by Laurent_R (Canon) on Nov 13, 2015 at 10:54 UTC
    You haven't answered my questions.

    The simple cases (just find if there is a difference or where the first difference is)can be dealt with something like this, which reads both files in parallel (assuming you have opened both files already):

    while (my $line1 = <$FILE1>) { my $line2 = <$FILE2>; print "$line1 missing in file2\n" and last unless defined $line2; next if $line1 eq $line2; print "Difference found on line $.\n"; # you may want to exit here, depending on what you need }
    It might be more complicated if you need to output more information.

    You may also want to add some code at the end to figure out if $FILE2 has more lines than $FILE1 at the end. But you haven't said enough on your requirement for me to be willing to look at all the edge cases.

    Please also note that there are some modules to do that, as you already know (judging from your original post).