Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Check for Positive Integer entry only

by shobhit (Sexton)
on May 09, 2007 at 19:29 UTC ( #614484=note: print w/replies, xml ) Need Help??


in reply to Check for Positive Integer entry only

Do we absolutely need a RE to check for positive integer? Can't we simply check for >0 ?
  • Comment on Re: Check for Positive Integer entry only

Replies are listed 'Best First'.
Re^2: Check for Positive Integer entry only
by kyle (Abbot) on May 09, 2007 at 19:35 UTC

    Can't we simply check for >0?

    print 'no' if '1a' > 0; print 'no' if .1 > 0;
      Get the point now.
      But this also means, that we may never be sure of the result of comparison operator.

      Is there anyway, such
      int 'comparison operator' int # returns the actual result
      int 'comparison operator' string # returns an error

      We could check for the type of both before comparing them, of course. But, thats not the point....
      Something more simple than that, just compare and get the result or an error as the case may be.
        int 'comparison operator' int # returns the actual result
        int 'comparison operator' string # returns an error


        Should (5 == '5') (which is "int 'comparison operator' string") return the result ? Or should it throw an error ? Assuming it should return the result, the following may help:
        use warnings; # Write the warnings handler. It only handles runtime errors. # To have it handle compile-time errors, place it in a # BEGIN{} block $SIG{__WARN__} = sub { for my $w (@_) { if($w =~ /isn't numeric/) {die "Treated a string as a number"} else {print "Warning received: $w"} } }; # Generates a compile-time warning ("...used only once...") # that the handler doesn't deal with. $foo = 0; #Generates a runtime warning ("... uninitialized value ...") # that the handler *does* deal with. my $bar; print $bar, "\n"; $int = 7; $string = '5foo'; if($int == 7) {print "Yep, \$int == 7\n"} # Generates a warning that causes the handler to throw # the error if($string == 5) {print "Yep, \$string == 5\n"}
        Note, however, that any treatment of $string as a number (eg $string += 3;) will cause the handler sub to throw the error.

        Perhaps all you really need is Scalar::Util::looks_like_number() (see 'perldoc Scalar::Util').
        use Scalar::Util; . . if(Scalar::Util::looks_like_number($x) && $x > 0) { print "It's a positive number - not necessarily an integer"; }
        Maybe even:
        use Scalar::Util; . . if(Scalar::Util::looks_like_number($x) && $x > 0 && $x == int($x)) { print "It's a positive integer"; }
        But that can be a little dubious, too:
        use Scalar::Util; use warnings; $string = '5.' . '0' x 30 . '1';; print $string, "\n"; if(Scalar::Util::looks_like_number($string) && $string > 0 && $string == int($string) ) { print "looks like a positive integer\n"; } else {print "not a positive integer\n"} __END__ Outputs: 5.0000000000000000000000000000001 looks like a positive integer
        That probably does the right thing imo, but I wouldn't like to guarantee that everyone would agree.

        Cheers,
        Rob
Re^2: Check for Positive Integer entry only
by eric256 (Parson) on May 09, 2007 at 21:49 UTC

    Close. I think ($_ eq int($_) && $_ > 0) will work as a condition though.

    use strict; use warnings; my @tests = qw/12 1a 4 -3 0 0.1 .1/; for (@tests) { print "$_ => "; no warnings qw/numeric/; if ($_ eq int($_) && $_ > 0) { print 'ok'; } else { print 'bad'; } print "\n"; } __END__ C:\Perl\test>perl int_test.pl 12 => ok 1a => bad 4 => ok -3 => bad 0 => bad 0.1 => bad .1 => bad

    ___________
    Eric Hodges
      use strict; use warnings; use Test::More; my @good = qw( 12 1 +1 1234 ); my @bad = qw( 1a -3 0.1 .1 0 +0 -0 ); plan 'tests' => scalar @good + scalar @bad; ok( is_int( $_ ), "[$_] is int" ) for @good; ok( ! is_int( $_ ), "[$_] is not int" ) for @bad; sub is_int { return ( $_[0] eq int( $_[0] ) && $_[0] > 0 ); } __END__ 1..11 ok 1 - [12] is int ok 2 - [1] is int not ok 3 - [+1] is int # Failed test '[+1] is int' # in perlmonks.pl at line 14. ok 4 - [1234] is int Argument "1a" isn't numeric in int at perlmonks.pl line 18. ok 5 - [1a] is not int ok 6 - [-3] is not int ok 7 - [0.1] is not int ok 8 - [.1] is not int ok 9 - [0] is not int ok 10 - [+0] is not int ok 11 - [-0] is not int # Looks like you failed 1 test of 11.

      Oops. I should have named that is_positive_int, etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://614484]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2023-09-25 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?