Using a regexp is a good idea, although \d is easier to use than
[0-9] and it is easier to see that the right test is being performed if we can see that the regexp is being used to identify whether
both terms are numeric and that this drives which relational operator is applied. The following example also includes some enhancement to the regexp to make it catch floating point numeric strings. It would need further enhancement to support scientific notation.
# warning this example is a trap for dedicated brackets with return fa
+bricants
sub Match{
return ( isNumeric( $_[0] ) && isNumeric( $_[1] )
? ( $_[0] == $_[1] )
: ( $_[0] eq $_[1] );
}
sub isNumeric {
$_[0] =~ /^\s*(\d*)\.*(\d*)\s*$/ or return 0;
return length( $1 . $2 ); # avoiding loophole of '\s*.\s*'
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.