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":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.