Sponsored Content
Top Forums Shell Programming and Scripting Check if a variable is a number - perl Post 302221701 by damiankaelgreen on Tuesday 5th of August 2008 03:14:55 AM
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";
}else{
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";
}else{
print "$thing is NOT a number!\n";
}
}

I'm still looking for more complete solutions though. If anyone has any better ideas, please post.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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: rolex.mp
4 Replies

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SDL::OpenGL(3)						User Contributed Perl Documentation					    SDL::OpenGL(3)

NAME
SDL::OpenGL - a perl extension DESCRIPTION
SDL::OpenGL is a perl module which when used by your application exports the gl* and glu* functions into your application's primary namespace. Most of the functions described in the OpenGL 1.3 specification are currently supported in this fashion. As the implementation of the OpenGL bindings that comes with SDL_perl is largely type agnositic, there is no need to decline the function names in the fashion that is done in the C API. For example, glVertex3d is simply glVertex, and perl just does the right thing with regards to types. CAVEATS
The following methods work different in Perl than in C: glCallLists glCallLists(@array_of_numbers); Unlike the C function, which get's passed a count, a type and a list of numbers, the Perl equivalent only takes a list of numbers. Note that this is slow, since it needs to allocate memory and construct a list of numbers from the given scalars. For a faster version see glCallListsString. The following methods exist in addition to the normal OpenGL specification: glCallListsString glCallListsString($string); Works like glCallLists(), except that it needs only one parameter, a scalar holding a string. The string is interpreted as a set of bytes, and each of these will be passed to glCallLists as GL_BYTE. This is faster than glCallLists, so you might want to pack your data like this: my $lists = pack("C", @array_of_numbers); And later use it like this: glCallListsString($lists); AUTHOR
David J. Goehrig SEE ALSO
perl SDL::App perl v5.12.1 2010-07-05 SDL::OpenGL(3)
All times are GMT -4. The time now is 02:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy