Solved

Is it a real number?

Posted on 2004-03-22
4
199 Views
Last Modified: 2008-03-06
I'm looking for a number somewhere between -10000 and 10000.

if (($_ < -9999) || ($_ >9999)) {
     print "\nIt's a number, but out of range\n";
} elsif ($_ >-10000) && ($_ <10000) {
    print "\nPerfect\n";
} elsif ($_ is an alpha/number or just contains alpha) {
    print"\nNumber expected, this contains other characters\n";
} else {print "\nShouldn't get here\n";}

I specifically need to know how to determine if $_ is a number or not.
0
Comment
Question by:ramble
4 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 200 total points
ID: 10653371
Very much depends on your definition of a number, but for the common case, you can use:

$var='abc';
print "$var is not a number\n" unless ($var =~ /\d+$/);
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 50 total points
ID: 10653437
unless ($_ =~ /\D/) { # proceed only if it's an integer

   if ($_ >= -10000  && $_ <= 10000) {
      print  "\nPerfect\n";
   }
   else {
      print "\nIt's a number, but out of range\n";
   }
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 10654197
perldoc -q "How do I determine whether a scalar is a number/whole/integer/float?"
0
 

Author Comment

by:ramble
ID: 10893523
Thanks_ramble

Just Ignore the last comment
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question