Check numeric variable

tlep Posted messages 597 Status Member -  
Char Snipeur Posted messages 10112 Registration date   Status Contributor Last intervention   -
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

b0rice Posted messages 9 Status Member 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
Char Snipeur Posted messages 10112 Registration date   Status Contributor Last intervention   1 331
 
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
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
 
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
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
 
ou
 perl -e '"aaa"=~/^\d+$/?"":print "non numérique\n"' 

--
106485010510997108
0
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
 
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
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
 
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
Char Snipeur Posted messages 10112 Registration date   Status Contributor Last intervention   1 331
 
Thank you for the clarification.
--
Greetings! I used to believe, now I am certain.Jesus Christ
Char Sniper
0