Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is it a real number?

Posted on 2004-03-22
4
Medium Priority
?
235 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 800 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 200 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 85

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

916 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