Pathologically Eclectic Rubbish Lister PerlMonks

### Re: Equality checking for strings AND numbers

by syphilis (Chancellor)
 on Jul 13, 2007 at 02:05 UTC ( #626353=note: print w/replies, xml ) Need Help??

in reply to Equality checking for strings AND numbers

Be aware that looks_like_number() returns true for strings like '9e0' and '9', but false for strings like '0x9'.

I don't know if it has any impact on what you are doing but your comp() subroutine will return true when comparing the numbers 9 and 0x9, will return true when comparing the strings '9e0' and '9', but will return false when comparing the strings '0x9' and '9' (or when comparing the string '0x9' to the number 9).
```use strict;
use warnings;
use Scalar::Util qw(looks_like_number);

my \$x1 = '0x9';
my \$x2 = 0x9;
my \$x3 =  '9';
my \$x4 = '9e0';
my \$x5 = 9e0;

print "1: ", comp(\$x1, 9), "\n";
print "2: ", comp(\$x2, 9), "\n";
print "3: ", comp(\$x3, 9), "\n";
print "4: ", comp(\$x4, 9), "\n";
print "5: ", comp(\$x5, 9), "\n";

sub comp {
my (\$a, \$b) = @_;
if (looks_like_number(\$a) && looks_like_number(\$b)) {
return (\$a == \$b);
}
else {
return (\$a eq \$b);
}
}

__END__

Outputs:
1:
2: 1
3: 1
4: 1
5: 1
Cheers,
Rob

Replies are listed 'Best First'.
Re^2: Equality checking for strings AND numbers
by Anonymous Monk on Jul 13, 2007 at 05:01 UTC
Thanks for the warning - all numerical values will be base10, sometimes in scientific format, so the looks_like_number call should work in this case.

So, looks_like_number only works for base10 (and below) numbers i.e. hexadecimal values with or without a trailing 0x will return false?

Although, of course not knowing the number base for numerical values will cause all kinds of other problems! ;)
On another note you could use Algorithm::Diff which would allow you to provide your own matching (or "key generation") function as they call it. This gets over the deficiencies of Text::Diff in only comparing text strings.
Looking at the Text::Diff module, I noticed the following:
```my \$diff = diff \&reader1,\&reader2;
I assume that this means you can use a subroutine to return the column you need from the input files and then just use Text::Diff to compare.

Do you have some sample input files? What sort of output are you expecting to be generated (a list of the differences, print to screen etc) and what should the format of this output be??
Here is some sample input.
```File1
-----
X  Y  Category1 Category2 Value1 Value2 Value3 Result
2  -9 1.0       2.0       1.1e3  1.234  -0.003 PASS

File2
-----
X  Y  Category2 Category1 Value3 Value1 Value2
2  -9 1         1         -0.003 1.1e3  1.2345 FAIL

The main points to note are:
1. The columns are not necessarily in the same order (the main reason I started this in the first place).
2. Data is a mixture of strings and numbers of differing precisions (but all base10).

To take account of the differences in column order, I rebuild the data using a hash keyed by the XY coordinate (first 2 columns) and using the actual column name e.g
```...other code omitted...
\$data1{"\$x,\$y}{\$colnames[\$colnum]} = \$linedata[\$colnum];

Output would be something like:
X=2 Y=9 Category2
X=2 Y=9 Value2
X=2 Y=9 Result

i.e. a list of the column names for which the data did not match between the two files, allowing for strings an numerical values, and accuracy to a certain precision (using the \$eps approach detailed elsewhere in this thread)

Create A New User
Node Status?
node history
Node Type: note [id://626353]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-08-19 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Asked to put a square peg in a round hole, I would:

Results (186 votes). Check out past polls.

Notices?