Solved

Is it a real number?

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 90
cpan issue 1 64
Question about @INC variable in perl 1 65
Perl string replace for refred url 9 63
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

791 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