Melderon has asked for the wisdom of the Perl Monks concerning the following question:

So I want to create an if statement that will not allow the input of a non-integer into STDIN. I tried two things that did not work:

The first:

if ($numind!=int) { print "**This is an invalid entry!", "\n", "Please try again.**\n" +; goto START; }

The second:

if ($numind!=int(1..10000)) { print "**This is an invalid entry!", "\n", "Please try again.**\n" +; goto START; }

The first error states that I have an uninitialized value, which makes sense but the second one, instead of limiting a decimal, limits all input, integer or not, and prints the "Invalid entry" output.

Any help is appreciated,

Melderon

Replies are listed 'Best First'.
Re: using int and the range function
by toolic (Bishop) on Jan 15, 2015 at 21:40 UTC
    One way using \D:
    use warnings; use strict; while (<DATA>) { chomp; print "This is an invalid entry: $_\n" if /\D/; } __DATA__ 555 3 1234 11-55 fadf 000 -402 1 2 1.2

Re: using int and the range function
by AnomalousMonk (Archbishop) on Jan 15, 2015 at 21:48 UTC
Re: using int and the range function
by Laurent_R (Canon) on Jan 15, 2015 at 22:42 UTC
    You could also use something like this:
    print "This is an integer\n" if int $input eq $input;
    Please note that it might at first glance seem more appropriate to use the numeric comparison operator ==, but this is not the case here. With the eq operator, you will detect that "4y" is not an integer, while with the == operator will silently do the conversion into an integer and report a wrong result.

    Having said that, I would probably tend to prefer some of the regexes already mentioned such as /^\d+$/ or, used negatively, /\D/.

    Je suis Charlie.

      I think this is closest to the OP's intent, because it also detects if the number doesn't fit into an "integer".

      But I'd change the if condition to
      ... if $input eq int $input;
      because for humans that's easier to parse than
      ... if int $input eq $input;
        Yes, you are right, it makes precedence comprehension a little bit easier. I actually thought of changing it the way you put it when I posted it, but since I tested it under the debugger the way I wrote it, I prefered not to change my post to something untested. In a real program, I would probably change it to make it clearer.
        Je suis Charlie.
Re: using int and the range function
by pme (Monsignor) on Jan 15, 2015 at 21:32 UTC
    You can check that using regexp:
    if ($numind !~ /^\d+/) { ... }
    Update: regexp is anchored to the beginning of the string.
      c:\@Work\Perl>perl -wMstrict -le "my $numind = '345xy'; if ($numind !~ /^\d+/) { die qq{'$numind' is not a number}; } print 'sum is ', 123 + $numind; " Argument "345xy" isn't numeric in addition (+) at -e line 1. sum is 468

      Give a man a fish:  <%-(-(-(-<

      Also need to make sure there is nothing after the digits:

      while (<>) { chomp; if (/^\d+$/) { print "$_ is an integer\n"; } }
        We would also take into account sign or '0x' prefix for hexadecimal numbers. This way we will end up reimplementing Scalar::Util::looks_like_number() what AnomalousMonk suggested below.
Re: using int and the range function
by Anonymous Monk on Jan 15, 2015 at 22:19 UTC
    Should't it also print "argument isn't numeric in int" for the second one?