You should probably avoid treating the values from your files as numbers.

It is possible, even likely, that the conversion of these values into Perl's internal numeric format would change their values. For example, they may have been truncated (or rounded using one of several different rounding algorithms), from single precision floats. If you then re-interpret them into double precision floats, you will introduce differences that are not there in the original files, or discard differences that are there.

This treats the fields as strings (as human eyes do) until the final decision about the last digits, where they are compared as (integer) numerics. It also takes every opportunity to bail out as early as a difference is found.

#! perl -slw use strict; die "Files differ in length" unless -s( $ARGV[0] ) == -s( $ARGV[ 0 ] ); open FH1, '<', $ARGV[0] or die $!; open FH2, '<', $ARGV[1] or die $!; #my $mismatch = 0; until( eof( FH1 ) || eof( FH2 ) ) { my $line1 = <FH1>; my $line2 = <FH2>; next if $line1 eq $line2; my @line1 = split ' ', $line1; my @line2 = split ' ', $line2; for ( 0 .. $#line1 ) { next if $line1[ $_ ] eq $line2[ $_ ]; next if abs( chop( $line1[ $_ ] ) - chop( $line2[ $_ ] ) ) < 2 and $line1[ $_ ] eq $line2[ $_ ]; die "Files differ at line: $. field: $_\n"; #$mismatch = 1; } } #die "File are different\n" if $mismatch; die "Files have different numbers of lines\n" unless eof( FH1 ) and eof( FH2 ); print "Files are the same\n"; ### Or "files are sufficiently similar" close FH1; close FH2;

Change the second die to warn and uncomment the related mismatch paraphernalia if you want a comprehensive list of the differences.

Output for test files:

C:\test>828506 file1 file2 Files are the same

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

In reply to Re: A small question on file comparison by BrowserUk
in thread A small question on file comparison by pavanvidem

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.