To answer your immediate question, instead of trying to guess what you really want to do: no, it's not quite correct. When you use == on arrays, you are comparing them in scalar context, and thus, you're numerically comparing the number of entries in each array. So yes, it'll return true if the files are the same, but not only then! All that is needed is that they contain the same number of lines.

Since perl 5.10.x, Perl has a new operator, the smart match operator, ~~ (see this tutorial), which might actually work in your case, and compare line by line. I don't know, I've not used it much, yet. Looking at that tutorial, it looks like it ought to.

There must be other solutions, for if you need it to work on an older perl. For example, take a look at how some Test modules do it, for example, Test::Deep, where you can simply compare arrays for equality, with the function cmp_deeply.

A really simple solution is to load the whole files into two scalars, instead of into arrays, and compare them as strings. Just set $/ to undef and you read the whole file as one line.

local $/; #sets to undef for the current scope open(DAT_Source, $Source_data_file) || die("Could not open file!"); $raw_data_source=<DAT_Source>; open(DAT_Expected, $Expected_data_file) || die("Could not open file!") +; $raw_data_Expected=<DAT_Expected>; if ($raw_data_source eq $raw_data_Expected) { print "DATA MATCHED!"; } else { print "DATA NOT MATCHED!"; }
That really requires very little change in your code.

That's one of the things I really love about Perl: you can often completely change how a piece of code works, by just changing a few thingies here and there.


In reply to Re: Two files comparasion by bart
in thread Two files comparasion by ganilmohan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.