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

Hi,
I am automating my test cases, that goes like this...
1) Save the output in a.txt
2) Compare the above file with b.txt.
So the code I have written is stated as below...


## READ THE OUPUT ACTUAL FILE
open(DAT_Source, $Actual_data_file) || die("Could not open file!");
my @raw_data_Actual=<DAT_Source>;
close(DAT_Source);
# READ THE OUPUT OF EXP FILE AND ASSIGN IT TO AN ARRAY
open(DAT_Expected, $Expected_data_file) || die("Could not open file!");
my @raw_data_Expected=<DAT_Expected>;
close(DAT_Expected);
#### VALIDATION OF THE TWO FILES ######
if (@raw_data_Actual == @raw_data_Expected)
{
print "$TCNumCurrent\t--\t$Pre1Current\tPass\n";
}
else
{
print "$TCNumCurrent\t--\t$Pre1Current\t--\t\t\t\tFail\n";
}

But this always gives the results as pass. It doesn't fail even if I change some text in b.txt. Help!!!!

Replies are listed 'Best First'.
Re: Comparing two text files
by Anonymous Monk on Aug 11, 2008 at 12:24 UTC
Re: Comparing two text files
by dHarry (Abbot) on Aug 11, 2008 at 12:17 UTC

    The problem is in (@raw_data_Actual == @raw_data_Expected).

    You compare in scalar context, 1==1 so always true.

    Changing the line to ("@raw_data_Actual" eq "@raw_data_Expected") should work.

    Frankly speaking I dont like this solution one bit. There are better solutions possible. I think you posted this question before (if I recall correctly) and some solutions were suggested to you!

Re: Comparing two text files
by starX (Chaplain) on Aug 11, 2008 at 13:58 UTC
Re: Comparing two text files
by GrandFather (Saint) on Aug 11, 2008 at 22:44 UTC

    You should find the answers to Two files comparasion helpful. Asking the same question twice is less useful than following up the replies you were given the first time with further questions to clarify anything you didn't understand.


    Perl reduces RSI - it saves typing
Re: Comparing two text files
by Anonymous Monk on Aug 11, 2008 at 12:18 UTC
    = is the assignment operator
Re: Comparing two text files
by prashanth_hsn (Initiate) on Aug 11, 2008 at 16:16 UTC
    Your comparision is in SCALAR context. So it would always compare only the number of elements in the Two arrays. So it's better to do it on line at a time.

    $index = 0; while($raw_data_Actual[$index] ne "" or $raw_data_Expected[$index] ne +""){ if($raw_data_Actual[$index] ne $raw_data_Expected[$index]){ print"Fail: $raw_data_Expected[$index]"; last; $index++; }


    This just one of the solution. There is always more than one way to solve a problem.