Check numeric variable

Hello,

I'm trying in Perl to check if a variable passed as an argument to a script is numeric.

I successfully tested:
if ($ARGV[2] =~ m/^\d+$/){print "Argument '".$ARGV[2]."' is numeric.\n";}

However, I'm unable to use NOT (to get the opposite):

if (!$ARGV[2] =~ m/^\d+$/){print "Argument '".$ARGV[2]."' is not numeric.\n";}
=> doesn't work
if ($ARGV[2] !=~ m/^\d+$/){print "Argument '".$ARGV[2]."' is not numeric.\n";}
=> doesn't work
if !($ARGV[2] =~ m/^\d+$/){print "Argument '".$ARGV[2]."' is not numeric.\n";}
=> doesn't work

Thank you :-)
Configuration: Windows XP Firefox 2.0.0.11

7 answers

  1. Contributor
    Thank you for the clarification.
    --
    Greetings! I used to believe, now I am certain.Jesus Christ
    Char Sniper
    0
    1. Moderator
      For the info

      !~ it's not quite a negation

      this operator rather means to find the non-match and not to not find the match
      --
      106485010510997108
      0
      1. Moderator
        Another possibility to test non-numeric is with the complemented character class \D that looks for non-numeric characters
        root@debian:~# perl -e 'print "non-numeric\n" if ("aaa" =~ /^\D+$/)' non-numeric
        this way I don't need to negate
        I simply look for a non-numeric match

        --
        106485010510997108
        0
        1. Moderator
          ou
           perl -e '"aaa"=~/^\d+$/?"":print "non numérique\n"' 

          --
          106485010510997108
          0
          1. Moderator
            Hello,

            here are some tests
            root@debian:~# perl -e 'print "non-numeric\n" if ("aaa" !~ /^\d+$') non-numeric root@debian:~# perl -e 'print "non-numeric\n" if (!("aaa" =~ /^\d+$/))' non-numeric root@debian:~# perl -e 'print "non-numeric\n" unless ("aaa" =~ /^\d+$/)' non-numeric 

            --
            106485010510997108
            0
            1. Contributor
              Salut.
              I don't understand why
              if (!$ARGV[2] =~ m/^\d+$/){print "Argument '".$ARGV[2]."' non numérique.\n";}
              doesn't work. Is it due to operator precedence? Would
              if (!($ARGV[2] =~ m/^\d+$/)){print "Argument '".$ARGV[2]."' non numérique.\n";}
              work? Otherwise, I'm really confused, the not on a boolean should always work normally.
              --
              Best regards! I used to believe, now I am sure.Jesus Christ
              Char Snipeur
              0
              1. Hello,

                the negation in a regexp in Perl is: $string !~ m/$re/;

                In your case:

                print 'Argument '.$ARGV[2].' non-numeric' if ($ARGV[2] !~ m/^\d+$/);

                You could also do:

                print "Argument ".$ARGV[2];
                if ( $ARGV[2] =~ m/^\d+$/ ) {
                print " numeric\n";
                }
                else {
                print " alphanumeric\n";
                }

                just simply ;)
                1