Solved

Is it a real number?

Posted on 2004-03-22
4
210 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

Technology Partners: 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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

740 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