When I ran this code, I got the following output:my $a = 3.0; my $b = '3.0'; my $stringCompareResult = $a eq $b?'true':'false'; my $numberCompareResult = $a == $b?'true':'false'; sub is_numeric { ($_[0] & ~ $_[0]) eq "0"; } print ("\$stringCompareResult: $stringCompareResult\n"); print ("\$numberCompareResult: $numberCompareResult\n"); my $isNumericResultA = is_numeric($a)?'true':'false'; my $isNumericResultB = is_numeric($b)?'true':'false'; print ("\$isNumericResultA: $isNumericResultA\n"); print ("\$isNumericResultB: $isNumericResultB\n");
I see, now, thanks to your reply, that when I used $a and $b in the earlier comparison, the interpreter CHANGED their "type". I had forgetting that Perl does this.$stringCompareResult: false $numberCompareResult: true $isNumericResultA: true $isNumericResultB: true
When I comment out the comparisons, and run just is_numeric, I get the same results as you. Thanks, bgreenlee!
In reply to Re^2: Testing For Numberness Vs Stringness
by BrooklineTom
in thread Testing For Numberness Vs Stringness
by BrooklineTom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |