Hi Mike,

You wrote:

>How do I test an input to see if it is a real number?  I have a situation
>something like:

(slightly snipped)

>if (input equals a real number) {
>        xxxxxx
>}
>else {
>xxxx
>}

Do you mean 'if it really is a number' or 'a real number' in the matematical
sense? If you have some advanced matematical things going on, please forget
this message, but if what you want to do is to check a value before
inserting it into a database or something similar to that, a quick and dirty
way of converting is just to say:

$value=<STDIN>;
$value*=1; # ie $value=$value*1;
if ($value){
   print "$value is a number\n"
}
else{
   print "This is not a number\n"
}

Divided by 1 or plus / minus 0 would do the same, force perl to think about
the value as a number. As a semi-beginner, I can see two problems with this
approach:
1: if $value=0 it will be counted as false
2: For a value that starts with a number, such as 123ABC, the numerical part
will be retained as a number, and everything including the first letter to
the end of the string will be thrown away:
i.e:
input  : ABCDE
output : This is not a number
input  : 0                   <- Zero
output : This is not a number
input  : 1.2345
output : 1.2345 is a number
input  : 123ABC
output : 123 is a number
input  : ABC123
output : This is not a number 

This may, or may not be, what you want. I am using that conversion/check
quite often, but in those cases, I know that my value cannot be zero.

Regards

Morten Sickel
Norwegian Radiation Protection Authority

Reply via email to