Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi,

I had a little play and noticed that your IsNumber() sub returns true for integers with embedded (and/or trailing) garbage, unless the garbage is alphabetic or whitespace.
Seemed odd ... but, of course, FAIK it's quite possible that the integer inputs you're receiving are either guaranteed to have no such garbage or are to be deemed acceptable if they include such garbage.
For the input array in the following script (where I also compare the IsNumber() and looks_like_number() results), IsNumber returns "1" for all but the final input.
use strict; use warnings; use Scalar::Util qw(looks_like_number); use Test::More; my @in = ('99/999998', '9999*9998', '9999-9998', '9999+9998', '9999:9998', '9999@:%?', '9999@:%?9998', '9999ABCD9998',); for(@in) { cmp_ok(IsNumber($_), '==', looks_like_number($_), "$_: " . IsNumber +($_)); } done_testing(); sub IsNumber { my ($string) = @_; my $valid = 0; my $count = $string =~ tr/\.//; if ( $string =~ m/[a-zA-Z\ \[\]]/ ) { $valid = 0; } elsif ( $string =~ /[^\x00-\x7F]/ ) { $valid = 0; } elsif ( $count > 1 ) { $valid = 0; } elsif ( $string =~ m/[#@':;><,.{}[]=!"£$%^&*()]/ ) { $valid = 0; } elsif ( $string =~ m/^[+-]?\d+$/ ) { $valid = 1; } elsif ( $string =~ m/^[+-]?[0-9]+[.]?[0-9]+/ ) { $valid = 1; } return $valid; }
Outputs:
not ok 1 - 99/999998: 1 # Failed test '99/999998: 1' # at try.pl line 13. # got: 1 # expected: not ok 2 - 9999*9998: 1 # Failed test '9999*9998: 1' # at try.pl line 13. # got: 1 # expected: not ok 3 - 9999-9998: 1 # Failed test '9999-9998: 1' # at try.pl line 13. # got: 1 # expected: not ok 4 - 9999+9998: 1 # Failed test '9999+9998: 1' # at try.pl line 13. # got: 1 # expected: not ok 5 - 9999:9998: 1 # Failed test '9999:9998: 1' # at try.pl line 13. # got: 1 # expected: not ok 6 - 9999@:%?: 1 # Failed test '9999@:%?: 1' # at try.pl line 13. # got: 1 # expected: not ok 7 - 9999@:%?9998: 1 # Failed test '9999@:%?9998: 1' # at try.pl line 13. # got: 1 # expected: ok 8 - 9999ABCD9998: 0 1..8 # Looks like you failed 7 tests of 8.
Cheers,
Rob

PS
I also did not expect that IsNumber('1.123e4') would return 0. But again, I don't know much about the possible inputs or the criteria for assessing them.

In reply to Re: Is A Number by syphilis
in thread Is A Number 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 making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found