Check numeric variable
tlep
Posted messages
597
Status
Member
-
Char Snipeur Posted messages 10112 Registration date Status Contributor Last intervention -
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 :-)
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
-
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 ;) -
Salut.
I don't understand whyif (!$ARGV[2] =~ m/^\d+$/){print "Argument '".$ARGV[2]."' non numérique.\n";}doesn't work. Is it due to operator precedence? Wouldif (!($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 -
Hello,
here are some testsroot@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 -
ou
perl -e '"aaa"=~/^\d+$/?"":print "non numérique\n"'
--
106485010510997108 -
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-numericthis way I don't need to negate
I simply look for a non-numeric match
--
106485010510997108 -
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 -
Thank you for the clarification.
--
Greetings! I used to believe, now I am certain.Jesus Christ
Char Sniper