parthu_r has asked for the wisdom of the Perl Monks concerning the following question:

I want to compare the content of two files without using diff.

Here is the code which I wrote

# # Compaing the output and the excepted # open FILE, "expected.txt" or die "can't open:$!"; @lines=<FILE>; $x=@lines; foreach $x1 (@lines) { $COMMAND1 =$x1 ; chomp($COMMAND1); } open FILE1, "output.txt" or die "can't openeee:$!"; @lines1=<FILE1>; $x1=@lines1; print $x1; foreach $x2 (@lines1) { $COMMAND2 =$x2 ; chomp($COMMAND2); } if($COMMAND1 eq $COMMAND2) { print "The clt has $COMMAND has Passed "; open(Trace1, ">>Passed.log"); $p1 ="The clt $COMMAND has passed \n"; print Trace1$p1,"\n"; } else { print "The clt $COMMAND has failed "; open(Trace2, ">>Failed.log"); $p2="The clt $COMMAND has failed \n"; print Trace2$p2,"\n"; }
But this code comapes the lastline in the array.

Is there any way I can compare the entire content.

Sarathy

Edit by dws to add <code> tags

Replies are listed 'Best First'.
Re: Compare the content of two files
by dws (Chancellor) on Jul 30, 2002 at 18:26 UTC
    The script has a logic problem. Your loops have the effect of leaving $COMMAND1 and $COMMAND2 holding the last line in their respective files. You also have another problem, which will be evident once you add   use strict; to the top of the script.

    An easy, but really ugly way to compare the two files is to change your compare to   if ( "@lines" eq "@lines1" ) { ... You can then do away with the "chomp()" loops.

    Algorithm::Diff is another approach you might consider.

Re: Compare the content of two files
by Ferret (Scribe) on Jul 30, 2002 at 18:30 UTC
    The obligatory answer is that I'd recommend using a module - for instance:

    Other than that, the above code is pretty mangled - you're reading in the whole file, then walking through it chomping lines, then comparing the end results of the loops. I'd take a little time to think about what you're trying to do in the above.

Re: Compare the content of two files
by Rich36 (Chaplain) on Jul 30, 2002 at 18:23 UTC