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 ;)
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 why
--
Best regards! I used to believe, now I am sure.Jesus Christ
Char Snipeur
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
Hello,
here are some tests
--
106485010510997108
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
Another possibility to test non-numeric is with the complemented character class \D that looks for non-numeric characters
I simply look for a non-numeric match
--
106485010510997108
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