Check if a variable is a number - perl

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if a variable is a number - perl
# 1  
Old 01-17-2008
Check if a variable is a number - perl

Logic of code

if ( $var is a number ) {
Do something
else {
Do something else

My question is: How do I check if a variable is a number. All the reg ex that I came up with to match this is failing. Please help.
# 2  
Old 01-17-2008
What pattern you need depends on what types of numbers you are expecting
- float, rational, integers, etc.

The following pattern works for integers which may or may not have a leading
plus or minus sign.

if ($var =~ /^[+-]?\d+$/ ) {
    print "Is a number\n";
} else {
    print "Is not a number\n";

# 3  
Old 01-17-2008
if you have Posix module
use POSIX;
@test = qw(zzz 456 vc32 xyz! 12.34);

foreach (@test) {
    print "$_ is digits\n" if isdigit $_;
    print "$_ is alpha\n" if isalpha $_;

# 4  
Old 08-05-2008
The POSIX example above only gives integers, not other real numbers...

Here's an alternative that I think may execute more quickly than using a regular expression or loading in an additional module and identifies decimal numbers... I'm sure there are probably faster ways, but this is the simplest that I can think of...

@list = qw(5 4.4 0 -1.2 asdf 12asdf asdf12 1.2e3 0e0);
foreach $thing (@list){
if (($thing * 1) eq $thing){
print "$thing is a number!\n";
print "$thing is NOT a number!\n";

Note that ("12asdf" * 1) equals 12...
Also note that the above example does NOT work for numbers in scientific notation.

The only way I can think of to make it work for numbers in sci-notation would be to use a regular expression. I don't like having to do that though because there is always room for exceptions when using regular expressions. Also, execution times could be much faster if there were a perlfunction "isreal" that returned the result; and since so many operations built into perl must have to know weather the scalar is real or not anyway, I don't see why it isn't already a function... For something as fundamental as this it really should be.
On another note, I saw a cpan module called Test:Smilieata::Scalar where it looks like somebody has started developing some scalar test functions, but the description still claims that the "number_ok" function still needs work because it basically only tests for digits right now.

Anyway, if your program can accept things like the "12asdf" as 12, then the following will probably work for scientific numbers (except for 0 when it is written in scientific notation, ex: "0e0").

@list = qw(5 4.4 0 -1.2 asdf 1.2e3asdf asdf12 1.2e3 0e0);
foreach $thing (@list){
$number_part = ($thing * 1);
$float_number = sprintf ("%e",$thing);
#note: in this case $thing gets interpreted to be the same as $number_part
if (($number_part eq $thing) or (($value != 0) and ($number_part ne $float_number)){
print "$thing is a number!\n";
print "$thing is NOT a number!\n";

I'm still looking for more complete solutions though. If anyone has any better ideas, please post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

2. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

3. Shell Programming and Scripting

PERL : check + or - sign in a variable

I have a variable $max = -3; It can be $max = +3; I need to check if this variable is a positive/negative value. if its positive, should print "positive" if not "negative" How can this be done? Thanks in advance (2 Replies)
Discussion started by: irudayaraj
2 Replies

4. Shell Programming and Scripting

number of fields in a text file as a variable - perl

I am looking for perl code to get following o/p. If a line has more than 7 fields then value in field 7 onwards is BHA_GRP1, BHA_GRP2, BHA_GRP3, BHA_GRP4 etc. Here is example of what I am trying to achieve. INPUT File: VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP1 BHA_GRP2 BHA_GRP3...... 2... (1 Reply)
Discussion started by: dynamax
1 Replies

5. Shell Programming and Scripting

Check if a variable ends in a particular number

Hi guys, I am working on a server where there are many users. The user names end in a 1 or a 2. I want to write a bash script that will say which users are in which group and was wondering if I could get some help. The only part I am unsure of is how to check if it ends in the number. Here's... (2 Replies)
Discussion started by: wua05
2 Replies

6. Shell Programming and Scripting

Perl Variable Check Script

I am working on a perl script that is used to update a list of hosts to a certain file but I am having an issue when I try to perform a check to make sure the user enters valid information. The following is what I have currently written for the script: IPINPUT: print "Enter IP Address: ";... (2 Replies)
Discussion started by: Takau
2 Replies

7. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

8. Shell Programming and Scripting

number check in perl

Hi,, this is returning true in all cases..( other than 10 dig number also) what could be wrong?? (2 Replies)
Discussion started by: shellwell
2 Replies

9. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by:
4 Replies

10. UNIX for Dummies Questions & Answers

Check if variable is a number

If I have a variable $X, how do I check it is a number? Many thanks. (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question