If you create a text file x.txt with following data
227.20
1176.00
262.00
481.60
492.00
489.60
808.50
934.50
150.00
806.00
563.70
295.60
552.90
327.25
0.00
0.00
0.00
Then file x2.txt
7566.85
And then run this code
open (X,"< x.txt");
while ($n = <X>) {
$n =~ s/\n|\r//g; # same with or without
$t += $n;
}
close X;
$st = `cat x2.txt`;
$st =~ s/\n|\r//g; # same with or without
print "t = $t\n";
print "st = $st\n";
if (equal($t,$st,5)) {
print "Equal\n";
} else {
print "Different\n";
}
sub equal {
my ($A,$B,$dp) = @_;
return sprintf("%.${dp}g",$A) eq sprintf("%.${dp}g",$B);
}
The result is
t = 7566.85
st = 7566.85
Different
So the only reliable solution I have found till now is
sub equal {
my ($A,$B,$dp) = @_;
if ($A == $B) {
return 1;
} elsif ($A eq $B) {
return 1;
}
return sprintf("%.${dp}g",$A) eq sprintf("%.${dp}g",$B);
}
Result
t = 7566.85
st = 7566.85
Equal
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.