I was thinking of something along the following lines:
use strict;
use Scalar::Util qw/looks_like_number/;
sub equal {
my ($first, $second) = @_;
if (
(! defined $first and ! defined $second) or
(looks_like_number($first) and looks_like_number($second) and
+$first == $second) or
(defined $first and defined $second and $first eq $second)
) {
return 1;
}
else {
return 0;
}
}
while (<DATA>) {
my ($first, $second, $result) = split /,/;
chomp $result;
if ($result == equal($first, $second)) {
print "OK: >$first< >$second< >$result<\n";
}
else {
print "NOT OK: >$first< >$second< expected >$result<; got >",e
+qual($first, $second),"<\n";
}
}
__DATA__
undef,undef,1
undef,,0
undef,0,0
,,1
100,100,1
100,99,0
100,0100,1
100,1E2,1
100,100.00,1
1000,1_000,1
1000,1_000.00,1
1000,1 000,1
1000,1 000.00,1
string,string,1
string,another_string,0
It seems to work except for the "funny" numbers written with underscores or spaces.
CountZero
"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
-
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.