Solved

Is it a real number?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now