Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Equality checking for strings AND numbers by syphilis
in thread Equality checking for strings AND numbers by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found